|
| 1 | +package search |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestParseDefines(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + input string |
| 10 | + want map[string]int |
| 11 | + }{ |
| 12 | + {"WIN32=1 DEBUG=0", map[string]int{"WIN32": 1, "DEBUG": 0}}, |
| 13 | + {"NDEBUG", map[string]int{"NDEBUG": 1}}, |
| 14 | + {"A=1 B=2 C", map[string]int{"A": 1, "B": 2, "C": 1}}, |
| 15 | + {"", map[string]int{}}, |
| 16 | + } |
| 17 | + for _, tt := range tests { |
| 18 | + got := ParseDefines(tt.input) |
| 19 | + if len(got) != len(tt.want) { |
| 20 | + t.Errorf("ParseDefines(%q): got %v, want %v", tt.input, got, tt.want) |
| 21 | + continue |
| 22 | + } |
| 23 | + for k, v := range tt.want { |
| 24 | + if got[k] != v { |
| 25 | + t.Errorf("ParseDefines(%q)[%q]: got %d, want %d", tt.input, k, got[k], v) |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestEvalIfExpr(t *testing.T) { |
| 32 | + defines := map[string]int{"WIN32": 1, "DEBUG": 0, "VERSION": 2} |
| 33 | + |
| 34 | + tests := []struct { |
| 35 | + expr string |
| 36 | + want bool |
| 37 | + }{ |
| 38 | + {"WIN32", true}, |
| 39 | + {"DEBUG", false}, |
| 40 | + {"!WIN32", false}, |
| 41 | + {"!DEBUG", true}, |
| 42 | + {"WIN32 == 1", true}, |
| 43 | + {"WIN32 == 0", false}, |
| 44 | + {"VERSION == 2", true}, |
| 45 | + {"WIN32 && !DEBUG", true}, |
| 46 | + {"WIN32 || DEBUG", true}, |
| 47 | + {"DEBUG || 0", false}, |
| 48 | + {"defined(WIN32)", true}, |
| 49 | + {"defined(UNKNOWN)", false}, |
| 50 | + {"(WIN32 && DEBUG) || VERSION", true}, |
| 51 | + } |
| 52 | + for _, tt := range tests { |
| 53 | + got := evalIfExpr(tt.expr, defines) |
| 54 | + if got != tt.want { |
| 55 | + t.Errorf("evalIfExpr(%q): got %v, want %v", tt.expr, got, tt.want) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestComputeInactiveSet(t *testing.T) { |
| 61 | + t.Run("ifdef active branch", func(t *testing.T) { |
| 62 | + lines := []string{ |
| 63 | + "#ifdef WIN32", |
| 64 | + "int x = 1;", |
| 65 | + "#else", |
| 66 | + "int x = 2;", |
| 67 | + "#endif", |
| 68 | + } |
| 69 | + defines := map[string]int{"WIN32": 1} |
| 70 | + inactive := computeInactiveSet(lines, defines) |
| 71 | + if inactive[2] { |
| 72 | + t.Error("line 2 should be active (WIN32 branch)") |
| 73 | + } |
| 74 | + if !inactive[4] { |
| 75 | + t.Error("line 4 should be inactive (else branch)") |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("ifdef inactive branch", func(t *testing.T) { |
| 80 | + lines := []string{ |
| 81 | + "#ifdef LINUX", |
| 82 | + "int x = 1;", |
| 83 | + "#else", |
| 84 | + "int x = 2;", |
| 85 | + "#endif", |
| 86 | + } |
| 87 | + defines := map[string]int{"WIN32": 1} |
| 88 | + inactive := computeInactiveSet(lines, defines) |
| 89 | + if !inactive[2] { |
| 90 | + t.Error("line 2 should be inactive (LINUX not defined)") |
| 91 | + } |
| 92 | + if inactive[4] { |
| 93 | + t.Error("line 4 should be active (else branch)") |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("ifndef", func(t *testing.T) { |
| 98 | + lines := []string{ |
| 99 | + "#ifndef NDEBUG", |
| 100 | + "assert(x);", |
| 101 | + "#endif", |
| 102 | + } |
| 103 | + defines := map[string]int{} |
| 104 | + inactive := computeInactiveSet(lines, defines) |
| 105 | + if inactive[2] { |
| 106 | + t.Error("line 2 should be active (NDEBUG not defined)") |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("nested ifdef", func(t *testing.T) { |
| 111 | + lines := []string{ |
| 112 | + "#ifdef WIN32", |
| 113 | + "#ifdef DEBUG", |
| 114 | + "log();", |
| 115 | + "#endif", |
| 116 | + "#endif", |
| 117 | + } |
| 118 | + defines := map[string]int{"WIN32": 1} |
| 119 | + inactive := computeInactiveSet(lines, defines) |
| 120 | + if !inactive[3] { |
| 121 | + t.Error("line 3 should be inactive (DEBUG not defined)") |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + t.Run("elif", func(t *testing.T) { |
| 126 | + lines := []string{ |
| 127 | + "#if VERSION == 1", |
| 128 | + "v1();", |
| 129 | + "#elif VERSION == 2", |
| 130 | + "v2();", |
| 131 | + "#else", |
| 132 | + "vx();", |
| 133 | + "#endif", |
| 134 | + } |
| 135 | + defines := map[string]int{"VERSION": 2} |
| 136 | + inactive := computeInactiveSet(lines, defines) |
| 137 | + if !inactive[2] { |
| 138 | + t.Error("line 2 should be inactive (VERSION != 1)") |
| 139 | + } |
| 140 | + if inactive[4] { |
| 141 | + t.Error("line 4 should be active (VERSION == 2)") |
| 142 | + } |
| 143 | + if !inactive[6] { |
| 144 | + t.Error("line 6 should be inactive (else)") |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
0 commit comments