github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/preprocessor/parse_include_preprocessor_line_test.go (about) 1 package preprocessor 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func TestParseIncludePreproccessorLine(t *testing.T) { 9 testCases := []struct { 10 inputLine string 11 out entity 12 }{ 13 { 14 inputLine: `# 1 "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h" 1 3 4`, 15 out: entity{ 16 include: "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h", 17 positionInSource: 1, 18 }, 19 }, 20 { 21 inputLine: `# 26 "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h" 3 4`, 22 out: entity{ 23 include: "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h", 24 positionInSource: 26, 25 }, 26 }, 27 { 28 inputLine: `# 854 "/usr/include/stdio.h" 2 3 4`, 29 out: entity{ 30 include: "/usr/include/stdio.h", 31 positionInSource: 854, 32 }, 33 }, 34 { 35 inputLine: `# 2 "f.c" 2`, 36 out: entity{ 37 include: "f.c", 38 positionInSource: 2, 39 }, 40 }, 41 { 42 inputLine: `# 2 "f.c"`, 43 out: entity{ 44 include: "f.c", 45 positionInSource: 2, 46 }, 47 }, 48 { 49 inputLine: `# 30 "/usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include/stdarg.h" 3 4`, 50 out: entity{ 51 include: "/usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include/stdarg.h", 52 positionInSource: 30, 53 }, 54 }, 55 { 56 inputLine: `# 3 "/home/lepricon/go/src/github.com/Konstantin8105/c4go/build/git-source/VasielBook/\320\223\320\273\320\260\320\262\320\260 6/6.2/main.c" 2`, 57 out: entity{ 58 include: `/home/lepricon/go/src/github.com/Konstantin8105/c4go/build/git-source/VasielBook/\320\223\320\273\320\260\320\262\320\260 6/6.2/main.c`, 59 positionInSource: 3, 60 }, 61 }, 62 } 63 for i, tc := range testCases { 64 t.Run(fmt.Sprintf("Test:%d", i), func(t *testing.T) { 65 actual, err := parseIncludePreprocessorLine(tc.inputLine) 66 if err != nil { 67 t.Fatal(err) 68 } 69 if len(actual.include) == 0 { 70 t.Fatal("Cannot parse, because result is empty") 71 } 72 if actual.include != tc.out.include { 73 t.Fatalf("Cannot parse line: \"%s\".\nResult: \"%s\".\nExpected: \"%s\"", tc.inputLine, actual.include, tc.out.include) 74 } 75 if actual.positionInSource != tc.out.positionInSource { 76 t.Fatalf("Cannot parse source position in line: \"%s\".\nResult: \"%d\".\nExpected: \"%d\"", tc.inputLine, actual.positionInSource, tc.out.positionInSource) 77 } 78 }) 79 } 80 } 81 82 func TestParseIncludePreproccessorLineFail1(t *testing.T) { 83 inputLine := `# A "/usr/include/stdio.h" 2 3 4` 84 _, err := parseIncludePreprocessorLine(inputLine) 85 if err == nil { 86 t.Fatal("Cannot found error in positionInSource") 87 } 88 } 89 90 func TestParseIncludePreproccessorLineFail2(t *testing.T) { 91 inputLine := ` # 850 "/usr/include/stdio.h" 2 3 4` 92 _, err := parseIncludePreprocessorLine(inputLine) 93 if err == nil { 94 t.Fatal("Cannot give error if first symbol is not #") 95 } 96 } 97 98 func TestParseIncludePreproccessorLineFail3(t *testing.T) { 99 inputLine := `# 850` 100 _, err := parseIncludePreprocessorLine(inputLine) 101 if err == nil { 102 t.Fatal("Cannot give error if line hanen't include string") 103 } 104 } 105 106 func TestParseIncludePreproccessorLineFail4(t *testing.T) { 107 inputLine := `# 850 "/usr/include` 108 _, err := parseIncludePreprocessorLine(inputLine) 109 if err == nil { 110 t.Fatal("Cannot give error if wrong format of include line") 111 } 112 } 113 114 func TestParseIncludePreproccessorLineFail5(t *testing.T) { 115 inputLine := `# 850` 116 _, err := parseIncludePreprocessorLine(inputLine) 117 if err == nil { 118 t.Fatal("Cannot give error if haven`t include line") 119 } 120 }