| 1 | package sixel |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestWriteRepeat(t *testing.T) { |
| 9 | tests := []struct { |
| 10 | name string |
| 11 | count int |
| 12 | char byte |
| 13 | expected string |
| 14 | wantErr bool |
| 15 | }{ |
| 16 | { |
| 17 | name: "basic repeat", |
| 18 | count: 3, |
| 19 | char: 'A', |
| 20 | expected: "!3A", |
| 21 | }, |
| 22 | { |
| 23 | name: "single digit", |
| 24 | count: 5, |
| 25 | char: '#', |
| 26 | expected: "!5#", |
| 27 | }, |
| 28 | { |
| 29 | name: "multiple digits", |
| 30 | count: 123, |
| 31 | char: 'x', |
| 32 | expected: "!123x", |
| 33 | }, |
| 34 | { |
| 35 | name: "zero count", |
| 36 | count: 0, |
| 37 | char: 'B', |
| 38 | expected: "!0B", |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | for _, tt := range tests { |
| 43 | t.Run(tt.name, func(t *testing.T) { |
| 44 | buf := &bytes.Buffer{} |
| 45 | n, err := WriteRepeat(buf, tt.count, tt.char) |
| 46 | if (err != nil) != tt.wantErr { |
| 47 | t.Errorf("WriteRepeat() error = %v, wantErr %v", err, tt.wantErr) |
| 48 | return |
| 49 | } |
| 50 | if got := buf.String(); got != tt.expected { |
| 51 | t.Errorf("WriteRepeat() = %v, want %v", got, tt.expected) |
| 52 | } |
| 53 | if n != len(tt.expected) { |
| 54 | t.Errorf("WriteRepeat() returned length = %v, want %v", n, len(tt.expected)) |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestDecodeRepeat(t *testing.T) { |
| 61 | tests := []struct { |
| 62 | name string |
| 63 | input []byte |
| 64 | wantRepeat Repeat |
| 65 | wantN int |
| 66 | description string |
| 67 | }{ |
| 68 | { |
| 69 | name: "basic repeat", |
| 70 | input: []byte("!3A"), |
| 71 | wantRepeat: Repeat{Count: 3, Char: 'A'}, |
| 72 | wantN: 3, |
| 73 | description: "simple single digit repeat", |
| 74 | }, |
| 75 | { |
| 76 | name: "multiple digits", |
| 77 | input: []byte("!123x"), |
| 78 | wantRepeat: Repeat{Count: 123, Char: 'x'}, |
| 79 | wantN: 5, |
| 80 | description: "repeat with multiple digits", |
| 81 | }, |
| 82 | { |
| 83 | name: "empty input", |
| 84 | input: []byte{}, |
| 85 | wantRepeat: Repeat{}, |
| 86 | wantN: 0, |
| 87 | description: "empty input should return zero values", |
| 88 | }, |
| 89 | { |
| 90 | name: "invalid introducer", |
| 91 | input: []byte("X3A"), |
| 92 | wantRepeat: Repeat{}, |
| 93 | wantN: 0, |
| 94 | description: "input without proper introducer", |
| 95 | }, |
| 96 | { |
| 97 | name: "incomplete sequence", |
| 98 | input: []byte("!3"), |
| 99 | wantRepeat: Repeat{}, |
| 100 | wantN: 0, |
| 101 | description: "incomplete sequence without character", |
| 102 | }, |
| 103 | } |
| 104 | |
| 105 | for _, tt := range tests { |
| 106 | t.Run(tt.name, func(t *testing.T) { |
| 107 | gotRepeat, gotN := DecodeRepeat(tt.input) |
| 108 | if gotRepeat != tt.wantRepeat { |
| 109 | t.Errorf("DecodeRepeat() gotRepeat = %v, want %v", gotRepeat, tt.wantRepeat) |
| 110 | } |
| 111 | if gotN != tt.wantN { |
| 112 | t.Errorf("DecodeRepeat() gotN = %v, want %v", gotN, tt.wantN) |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestRepeat_String(t *testing.T) { |
| 119 | tests := []struct { |
| 120 | name string |
| 121 | repeat Repeat |
| 122 | expected string |
| 123 | }{ |
| 124 | { |
| 125 | name: "basic repeat", |
| 126 | repeat: Repeat{Count: 3, Char: 'A'}, |
| 127 | expected: "!3A", |
| 128 | }, |
| 129 | { |
| 130 | name: "multiple digits", |
| 131 | repeat: Repeat{Count: 123, Char: 'x'}, |
| 132 | expected: "!123x", |
| 133 | }, |
| 134 | { |
| 135 | name: "zero count", |
| 136 | repeat: Repeat{Count: 0, Char: 'B'}, |
| 137 | expected: "!0B", |
| 138 | }, |
| 139 | } |
| 140 | |
| 141 | for _, tt := range tests { |
| 142 | t.Run(tt.name, func(t *testing.T) { |
| 143 | if got := tt.repeat.String(); got != tt.expected { |
| 144 | t.Errorf("Repeat.String() = %v, want %v", got, tt.expected) |
| 145 | } |
| 146 | }) |
| 147 | } |
| 148 | } |