@@ -8,7 +8,11 @@ const { findNextSignatureBlock } = require('../utils');
88describe ( 'generateDocComment()' , ( ) => {
99 it ( 'generates doc for function' , ( ) => {
1010 const input = 'pub fn compute_sum(a: i32, b: i32) -> i32 {' ;
11- const output = generateDocComment ( input ) ;
11+ const output = generateDocComment ( input , {
12+ includeExamples : true ,
13+ examplesOnlyForPublicOrExtern : false ,
14+ includeSafetyDetails : true
15+ } ) ;
1216 assert . ok ( output . includes ( '# Arguments' ) ) ;
1317 assert . ok ( output . includes ( '`a` (`i32`)' ) ) ;
1418 assert . ok ( output . includes ( '# Returns' ) ) ;
@@ -17,28 +21,40 @@ describe('generateDocComment()', () => {
1721
1822 it ( 'generates doc for struct' , ( ) => {
1923 const input = 'pub struct Point { x: f64, y: f64 }' ;
20- const output = generateDocComment ( input ) ;
24+ const output = generateDocComment ( input , {
25+ includeExamples : true ,
26+ examplesOnlyForPublicOrExtern : false ,
27+ includeSafetyDetails : true
28+ } ) ;
2129 assert . ok ( output . includes ( '# Fields' ) ) ;
2230 assert . ok ( output . includes ( '`x` (`f64`)' ) ) ;
2331 assert . ok ( output . includes ( '`y` (`f64`)' ) ) ;
2432 } ) ;
2533
2634 it ( 'generates doc for enum' , ( ) => {
2735 const input = 'enum Color { Red, Green, Blue }' ;
28- const output = generateDocComment ( input ) ;
36+ const output = generateDocComment ( input , {
37+ includeExamples : true ,
38+ examplesOnlyForPublicOrExtern : false ,
39+ includeSafetyDetails : true
40+ } ) ;
2941 assert . ok ( output . includes ( '# Variants' ) ) ;
3042 assert . ok ( output . includes ( '`Red`' ) ) ;
3143 } ) ;
3244
3345 it ( 'returns null for unsupported input' , ( ) => {
34- assert . strictEqual ( generateDocComment ( 'trait SomeTrait {}' ) , null ) ;
46+ assert . strictEqual ( generateDocComment ( 'trait SomeTrait {}' , {
47+ includeExamples : true ,
48+ examplesOnlyForPublicOrExtern : false ,
49+ includeSafetyDetails : true
50+ } ) , null ) ;
3551 } ) ;
3652} ) ;
3753
3854describe ( 'generateFunctionDoc()' , ( ) => {
3955 it ( 'includes async/unsafe modifiers in example block' , ( ) => {
4056 const input = 'pub async unsafe fn risky_call() -> Result<(), Box<dyn Error>> {' ;
41- const doc = generateFunctionDoc ( input ) ;
57+ const doc = generateFunctionDoc ( input , true , true , true ) ;
4258 assert . ok ( doc . includes ( 'async {' ) ) ;
4359 assert . ok ( doc . includes ( 'unsafe {' ) ) ;
4460 assert . ok ( doc . includes ( '# Safety' ) ) ;
@@ -47,14 +63,14 @@ describe('generateFunctionDoc()', () => {
4763
4864 it ( 'handles simple function without return' , ( ) => {
4965 const input = 'fn hello(name: &str) {' ;
50- const doc = generateFunctionDoc ( input ) ;
66+ const doc = generateFunctionDoc ( input , true , true , true ) ;
5167 assert . ok ( doc . includes ( '`name` (`&str`)' ) ) ;
5268 assert . ok ( ! doc . includes ( '# Returns' ) ) ;
5369 } ) ;
5470
5571 it ( 'includes arguments and return section' , ( ) => {
5672 const input = 'pub fn add(a: i32, b: i32) -> i32 {' ;
57- const output = generateFunctionDoc ( input ) ;
73+ const output = generateFunctionDoc ( input , true , true , true ) ;
5874 assert . ok ( output . includes ( '# Arguments' ) , 'Missing arguments section' ) ;
5975 assert . ok ( output . includes ( '`a` (`i32`)' ) ) ;
6076 assert . ok ( output . includes ( '`b` (`i32`)' ) ) ;
@@ -64,74 +80,92 @@ describe('generateFunctionDoc()', () => {
6480
6581 it ( 'handles unsafe functions' , ( ) => {
6682 const input = 'pub unsafe fn access_raw(ptr: *const u8) -> u8 {' ;
67- const output = generateFunctionDoc ( input ) ;
83+ const output = generateFunctionDoc ( input , true , true , true ) ;
6884 assert . ok ( output . includes ( '# Safety' ) , 'Missing safety section' ) ;
6985 assert . ok ( output . includes ( '**This function is `unsafe` because:**' ) ) ;
7086 assert . ok ( output . includes ( 'Describe unsafe behavior' ) , 'Missing unsafe placeholder' ) ;
7187 } ) ;
7288
7389 it ( 'handles extern functions' , ( ) => {
7490 const input = 'pub extern "C" fn c_func(x: i32) -> i32 {' ;
75- const output = generateFunctionDoc ( input ) ;
91+ const output = generateFunctionDoc ( input , true , true , true ) ;
7692 assert . ok ( output . includes ( '# Safety' ) , 'Missing safety section' ) ;
7793 assert . ok ( output . includes ( 'called in the correct program state to avoid UB' ) , 'Missing UB safety line' ) ;
7894 } ) ;
7995
8096 it ( 'handles async functions' , ( ) => {
8197 const input = 'pub async fn fetch_data() -> Result<String, Box<dyn Error>> {' ;
82- const output = generateFunctionDoc ( input ) ;
98+ const output = generateFunctionDoc ( input , true , true , true ) ;
8399 assert . ok ( output . includes ( '# Errors' ) , 'Missing errors section' ) ;
84100 assert . ok ( output . includes ( 'fetch_data().await' ) , 'Missing async example' ) ;
85101 } ) ;
86102
87103 it ( 'handles async unsafe extern functions' , ( ) => {
88104 const input = 'pub async unsafe extern "C" fn full_danger() -> Result<(), MyError> {' ;
89- const output = generateFunctionDoc ( input ) ;
105+ const output = generateFunctionDoc ( input , true , true , true ) ;
90106 assert . ok ( output . includes ( '# Safety' ) , 'Missing safety section' ) ;
91107 assert . ok ( output . includes ( 'unsafe { full_danger().await }' ) , 'Missing async unsafe example' ) ;
92108 assert . ok ( output . includes ( '# Errors' ) , 'Missing errors section' ) ;
93109 } ) ;
94110
95111 it ( 'returns null on invalid input' , ( ) => {
96- const output = generateFunctionDoc ( 'this is not a function' ) ;
112+ const output = generateFunctionDoc ( 'this is not a function' , true , true , true ) ;
97113 assert . strictEqual ( output , null ) ;
98114 } ) ;
99115
100116 it ( 'handles pub(crate) visibility' , ( ) => {
101117 const input = 'pub(crate) fn public_function_in_crate() {' ;
102- const doc = generateFunctionDoc ( input ) ;
118+ const doc = generateFunctionDoc ( input , true , true , true ) ;
103119 assert . ok ( doc . includes ( 'Describe this function' ) , 'Missing main description' ) ;
104120 assert . ok ( doc . includes ( '# Examples' ) , 'Missing example section' ) ;
105121 } ) ;
106122
107123 it ( 'handles pub(in crate::testing) visibility' , ( ) => {
108124 const input = 'pub(in crate::testing) fn public_function_in_my_mod() {' ;
109- const doc = generateFunctionDoc ( input ) ;
125+ const doc = generateFunctionDoc ( input , true , true , true ) ;
110126 assert . ok ( doc . includes ( 'Describe this function' ) , 'Missing main description' ) ;
111127 } ) ;
112128
113129 it ( 'handles pub(self) visibility' , ( ) => {
114130 const input = 'pub(self) fn public_function_in_nested() {' ;
115- const doc = generateFunctionDoc ( input ) ;
131+ const doc = generateFunctionDoc ( input , true , true , true ) ;
116132 assert . ok ( doc . includes ( '# Examples' ) , 'Missing example section' ) ;
117133 } ) ;
118134
119135 it ( 'handles pub(super) visibility' , ( ) => {
120136 const input = 'pub(super) fn public_function_in_super_mod() {' ;
121- const doc = generateFunctionDoc ( input ) ;
137+ const doc = generateFunctionDoc ( input , true , true , true ) ;
122138 assert . ok ( doc . includes ( 'Describe this function' ) , 'Missing placeholder description' ) ;
123139 } ) ;
140+
141+ it ( 'respects includeExamples = false and hides examples section' , ( ) => {
142+ const input = 'fn private_internal() {}' ;
143+ const doc = generateFunctionDoc ( input , false , false , true ) ;
144+ assert . ok ( ! doc . includes ( '# Examples' ) , 'Should not include example section for private function' ) ;
145+ } ) ;
146+
147+ it ( 'respects examplesOnlyForPublicOrExtern = true and hides examples for private function' , ( ) => {
148+ const input = 'fn private_internal() {}' ;
149+ const doc = generateFunctionDoc ( input , true , true , true ) ;
150+ assert . ok ( ! doc . includes ( '# Examples' ) , 'Should not include example section for private function' ) ;
151+ } ) ;
152+
153+ it ( 'includes examples when examplesOnlyForPublicOrExtern = true and function is pub' , ( ) => {
154+ const input = 'pub fn exposed() {}' ;
155+ const doc = generateFunctionDoc ( input , true , true , true ) ;
156+ assert . ok ( doc . includes ( '# Examples' ) , 'Should include example section for public function' ) ;
157+ } ) ;
124158} ) ;
125159
126160describe ( 'generateStructDoc()' , ( ) => {
127161 it ( 'returns null for unit struct' , ( ) => {
128- assert . strictEqual ( generateStructDoc ( 'struct Empty;' ) , null ) ;
129- assert . strictEqual ( generateStructDoc ( 'struct Nothing {}' ) , null ) ;
162+ assert . strictEqual ( generateStructDoc ( 'struct Empty;' , true , true ) , null ) ;
163+ assert . strictEqual ( generateStructDoc ( 'struct Nothing {}' , true , true ) , null ) ;
130164 } ) ;
131165
132166 it ( 'documents tuple struct' , ( ) => {
133167 const input = 'struct Pair(i32, f64);' ;
134- const doc = generateStructDoc ( input ) ;
168+ const doc = generateStructDoc ( input , true , false ) ;
135169 assert . ok ( doc . includes ( '`field_0` (`i32`)' ) ) ;
136170 assert . ok ( doc . includes ( '`field_1` (`f64`)' ) ) ;
137171 assert . ok ( doc . includes ( '# Examples' ) ) ;
@@ -146,7 +180,7 @@ describe('generateStructDoc()', () => {
146180 p: i32, q: i32, r: i32, s: i32, t: i32
147181 }
148182 ` ;
149- const doc = generateStructDoc ( input ) ;
183+ const doc = generateStructDoc ( input , true , true ) ;
150184 assert . ok ( doc . includes ( '# Fields' ) , 'Missing fields section' ) ;
151185 assert . ok ( ( doc . match ( / - ` / g) || [ ] ) . length >= 20 , 'Should document 20+ fields' ) ;
152186 } ) ;
@@ -161,7 +195,7 @@ describe('generateEnumDoc()', () => {
161195 Write(String),
162196 }
163197 ` ;
164- const doc = generateEnumDoc ( input ) ;
198+ const doc = generateEnumDoc ( input , true , false ) ;
165199 assert . ok ( doc . includes ( '`Quit`' ) ) ;
166200 assert . ok ( doc . includes ( '`Move { x, y }`' ) ) ;
167201 assert . ok ( doc . includes ( '`Write(String)`' ) ) ;
@@ -170,7 +204,7 @@ describe('generateEnumDoc()', () => {
170204
171205 it ( 'returns null for invalid enum' , ( ) => {
172206 const badEnum = 'enum NotValid' ;
173- assert . strictEqual ( generateEnumDoc ( badEnum ) , null ) ;
207+ assert . strictEqual ( generateEnumDoc ( badEnum , true , true ) , null ) ;
174208 } ) ;
175209
176210 it ( 'handles enum with mixed variant types' , ( ) => {
@@ -182,7 +216,7 @@ describe('generateEnumDoc()', () => {
182216 ChangeColor(i32, i32, i32),
183217 }
184218 ` ;
185- const doc = generateEnumDoc ( input ) ;
219+ const doc = generateEnumDoc ( input , true , true ) ;
186220 assert . ok ( doc . includes ( '# Variants' ) , 'Missing variants section' ) ;
187221 assert . ok ( doc . includes ( '- `Quit`' ) , 'Missing unit variant' ) ;
188222 assert . ok ( doc . includes ( '- `Move { x, y }`' ) , 'Missing struct variant' ) ;
@@ -197,7 +231,7 @@ describe('generateEnumDoc()', () => {
197231 Field { a: i32, b: i32, c: i32, d: i32, e: i32 }
198232 }
199233 ` ;
200- const doc = generateEnumDoc ( input ) ;
234+ const doc = generateEnumDoc ( input , true , true ) ;
201235 assert . ok ( doc . includes ( 'Tuple(u8, u16, u32, u64, usize)' ) , 'Missing tuple variant formatting' ) ;
202236 assert . ok ( doc . includes ( 'Field { a, b, c, d, e }' ) , 'Missing field variant formatting' ) ;
203237 } ) ;
0 commit comments