|
| 1 | +package struct2ts |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestTabScanner_Write(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + Name string |
| 11 | + Input struct { |
| 12 | + Tabs string |
| 13 | + Buffers []string |
| 14 | + } |
| 15 | + Expected struct { |
| 16 | + Output string |
| 17 | + n *int |
| 18 | + } |
| 19 | + }{ |
| 20 | + { |
| 21 | + "One new line mid string - one input, 3 spaces", |
| 22 | + struct { |
| 23 | + Tabs string |
| 24 | + Buffers []string |
| 25 | + }{Tabs: " ", Buffers: []string{"Hello how are you?\nI'm fine thanks!"}}, |
| 26 | + struct { |
| 27 | + Output string |
| 28 | + n *int |
| 29 | + }{Output: "Hello how are you?\n I'm fine thanks!"}, |
| 30 | + }, |
| 31 | + { |
| 32 | + "One new line end string - one input, 3 spaces", |
| 33 | + struct { |
| 34 | + Tabs string |
| 35 | + Buffers []string |
| 36 | + }{Tabs: " ", Buffers: []string{"Hello how are you?\n"}}, |
| 37 | + struct { |
| 38 | + Output string |
| 39 | + n *int |
| 40 | + }{Output: "Hello how are you?\n "}, |
| 41 | + }, |
| 42 | + { |
| 43 | + "One new line start string - one input, 3 spaces", |
| 44 | + struct { |
| 45 | + Tabs string |
| 46 | + Buffers []string |
| 47 | + }{Tabs: " ", Buffers: []string{"\nHello how are you?"}}, |
| 48 | + struct { |
| 49 | + Output string |
| 50 | + n *int |
| 51 | + }{Output: "\n Hello how are you?"}, |
| 52 | + }, |
| 53 | + { |
| 54 | + "empty string", |
| 55 | + struct { |
| 56 | + Tabs string |
| 57 | + Buffers []string |
| 58 | + }{Tabs: " ", Buffers: []string{""}}, |
| 59 | + struct { |
| 60 | + Output string |
| 61 | + n *int |
| 62 | + }{Output: ""}, |
| 63 | + }, |
| 64 | + { |
| 65 | + "Just a new line", |
| 66 | + struct { |
| 67 | + Tabs string |
| 68 | + Buffers []string |
| 69 | + }{Tabs: " ", Buffers: []string{"\n"}}, |
| 70 | + struct { |
| 71 | + Output string |
| 72 | + n *int |
| 73 | + }{Output: "\n "}, |
| 74 | + }, |
| 75 | + } |
| 76 | + for _, test := range tests { |
| 77 | + t.Run(test.Name, func(t *testing.T) { |
| 78 | + outputBuffer := bytes.NewBuffer(nil) |
| 79 | + ts := newTabScanner(outputBuffer, test.Input.Tabs) |
| 80 | + for _, s := range test.Input.Buffers { |
| 81 | + _, err := ts.Write([]byte(s)) |
| 82 | + if err != nil { |
| 83 | + t.Error("Buffer write error", err) |
| 84 | + } |
| 85 | + } |
| 86 | + if outputBuffer.String() != test.Expected.Output { |
| 87 | + t.Log("Output buffer doesn't match. Got ") |
| 88 | + t.Log(outputBuffer.String()) |
| 89 | + t.Log("expected") |
| 90 | + t.Log(test.Expected.Output) |
| 91 | + t.Fail() |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
0 commit comments