@@ -5,6 +5,52 @@ use codebook::{
55
66mod utils;
77
8+ /// Strategy:
9+ /// Use distinct misspellings to test location sensitive checking.
10+ /// This simpler to write than asserting exact locations.
11+ /// Granted - it doesn't test that the spell_check return correct locations,
12+ /// but should be sufficient that some tests tests this.
13+ /// This can be used to test language-specific grammar rules with less effort.
14+ ///
15+ /// `not_expected` does not have to be exhaustive.
16+ fn assert_simple_misspellings (
17+ processor : & codebook:: Codebook ,
18+ sample_text : & str ,
19+ expected_misspellings : Vec < & str > ,
20+ not_expected : Vec < & str > ,
21+ language : LanguageType ,
22+ ) {
23+ // Check that the misspelled words used are distinct,
24+ // otherwise the test could fail to properly test location sensitive properties
25+ for word in expected_misspellings. iter ( ) {
26+ let count = sample_text. matches ( word) . count ( ) ;
27+ assert_eq ! (
28+ count, 1 ,
29+ "Word '{}' should occur exactly once in sample_text, but found {} occurrences" ,
30+ word, count
31+ ) ;
32+ }
33+
34+ let binding = processor
35+ . spell_check ( sample_text, Some ( language) , None )
36+ . to_vec ( ) ;
37+ let mut misspelled = binding
38+ . iter ( )
39+ . map ( |r| r. word . as_str ( ) )
40+ . collect :: < Vec < & str > > ( ) ;
41+ misspelled. sort ( ) ;
42+ println ! ( "Misspelled words: {misspelled:?}" ) ;
43+
44+ let mut expected_misspellings_sorted = expected_misspellings. clone ( ) ;
45+ expected_misspellings_sorted. sort ( ) ;
46+ assert_eq ! ( misspelled, expected_misspellings_sorted) ;
47+
48+ for word in not_expected {
49+ println ! ( "Not expecting: {word:?}" ) ;
50+ assert ! ( !misspelled. iter( ) . any( |w| * w == word) ) ;
51+ }
52+ }
53+
854#[ test]
955fn test_python_simple ( ) {
1056 utils:: init_logging ( ) ;
@@ -298,3 +344,79 @@ fn test_python_import_statements() {
298344 println ! ( "Misspelled words: {misspelled:?}" ) ;
299345 assert_eq ! ( misspelled, expected) ;
300346}
347+
348+ #[ test]
349+ fn test_python_functions ( ) {
350+ utils:: init_logging ( ) ;
351+ let processor = utils:: get_processor ( ) ;
352+
353+ // Test simple function - function name and parameter names should be checked
354+ let simple_function = r#"
355+ def simple_wrngfunction_name(wrngparam, correct, wrngdefaultparam=1, correct_default=2):
356+ pass
357+ "# ;
358+ assert_simple_misspellings (
359+ & processor,
360+ simple_function,
361+ vec ! [ "wrngfunction" , "wrngparam" , "wrngdefaultparam" ] ,
362+ vec ! [ "simple" , "correct" , "def" , "name" , "default" ] ,
363+ LanguageType :: Python ,
364+ ) ;
365+
366+ // Test typed function - function names and parameters should be checked, but not types or modules
367+ let simple_typed_function = r#"
368+ def simple_wrngfunction(wrngparam: str, correct: Wrngtype, other: wrngmod.Wrngmodtype, correct_default: Nons | int = 2) -> Wrngret:
369+ pass
370+ "# ;
371+ assert_simple_misspellings (
372+ & processor,
373+ simple_typed_function,
374+ vec ! [ "wrngfunction" , "wrngparam" ] ,
375+ vec ! [
376+ "simple" ,
377+ "correct" ,
378+ "str" ,
379+ "Wrngtype" ,
380+ "wrngmod" ,
381+ "Wrngmodtype" ,
382+ "Wrngret" ,
383+ "def" ,
384+ "Nons" ,
385+ "default" ,
386+ ] ,
387+ LanguageType :: Python ,
388+ ) ;
389+
390+ // Test generic function 1 - function names and parameters should be checked, but not types
391+ let generic_function_1 = r#"
392+ def simple_wrngfunction(wrngparam: str, correct: Wrngtype[Wrngtemplate]):
393+ pass
394+ "# ;
395+ assert_simple_misspellings (
396+ & processor,
397+ generic_function_1,
398+ vec ! [ "wrngfunction" , "wrngparam" ] ,
399+ vec ! [ "simple" , "correct" , "str" , "Wrngtype" , "Wrngtemplate" ] ,
400+ LanguageType :: Python ,
401+ ) ;
402+
403+ // Test generic function 2 - function names and parameters should be checked, but not type templates
404+ let generic_function_2 = r#"
405+ def simple_wrngfunction[Wrgtemplate](wrngparam: str, correct: Wrngtype[Wrngtemplate]):
406+ pass
407+ "# ;
408+ assert_simple_misspellings (
409+ & processor,
410+ generic_function_2,
411+ vec ! [ "wrngfunction" , "wrngparam" ] ,
412+ vec ! [
413+ "simple" ,
414+ "correct" ,
415+ "str" ,
416+ "Wrgtemplate" ,
417+ "Wrngtype" ,
418+ "Wrngtemplate" ,
419+ ] ,
420+ LanguageType :: Python ,
421+ ) ;
422+ }
0 commit comments