| 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package runes |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | "unicode/utf8" |
| 20 | ) |
| 21 | |
| 22 | func TestNewBuffer_ASCII(t *testing.T) { |
| 23 | data := "hello world!" |
| 24 | rb := NewBuffer(data) |
| 25 | if got, want := rb.Len(), utf8.RuneCountInString(data); got != want { |
| 26 | t.Errorf("length mismatch: got %d, want %d", got, want) |
| 27 | } |
| 28 | if got, want := rb.Slice(0, rb.Len()), data; got != want { |
| 29 | t.Errorf("slice mismatch: got %q, want %q", got, want) |
| 30 | } |
| 31 | if got, want := rb.Get(8), rune('r'); got != want { |
| 32 | t.Errorf("rune mismatch: got %U, want %U", got, want) |
| 33 | } |
| 34 | if _, ok := rb.(*asciiBuffer); !ok { |
| 35 | t.Errorf("type mismatch: got %T, want %T", rb, &asciiBuffer{}) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestNewBuffer_Basic(t *testing.T) { |
| 40 | data := "hello w\u04E7rld!" |
| 41 | rb := NewBuffer(data) |
| 42 | if got, want := rb.Len(), utf8.RuneCountInString(data); got != want { |
| 43 | t.Errorf("length mismatch: got %d, want %d", got, want) |
| 44 | } |
| 45 | if got, want := rb.Slice(0, rb.Len()), data; got != want { |
| 46 | t.Errorf("slice mismatch: got %q, want %q", got, want) |
| 47 | } |
| 48 | if got, want := rb.Get(8), rune('r'); got != want { |
| 49 | t.Errorf("rune mismatch: got %U, want %U", got, want) |
| 50 | } |
| 51 | if _, ok := rb.(*basicBuffer); !ok { |
| 52 | t.Errorf("type mismatch: got %T, want %T", rb, &basicBuffer{}) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestNewBuffer_Supplemental(t *testing.T) { |
| 57 | data := "hello w\U0001F642rld!" |
| 58 | rb := NewBuffer(data) |
| 59 | if got, want := rb.Len(), utf8.RuneCountInString(data); got != want { |
| 60 | t.Errorf("length mismatch: got %d, want %d", got, want) |
| 61 | } |
| 62 | if got, want := rb.Slice(0, rb.Len()), data; got != want { |
| 63 | t.Errorf("slice mismatch: got %q, want %q", got, want) |
| 64 | } |
| 65 | if got, want := rb.Get(8), rune('r'); got != want { |
| 66 | t.Errorf("rune mismatch: got %U, want %U", got, want) |
| 67 | } |
| 68 | if _, ok := rb.(*supplementalBuffer); !ok { |
| 69 | t.Errorf("type mismatch: got %T, want %T", rb, &supplementalBuffer{}) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestNewBuffer_All(t *testing.T) { |
| 74 | data := "hell\u04E7 w\U0001F642rld!" |
| 75 | rb := NewBuffer(data) |
| 76 | if got, want := rb.Len(), utf8.RuneCountInString(data); got != want { |
| 77 | t.Errorf("length mismatch: got %d, want %d", got, want) |
| 78 | } |
| 79 | if got, want := rb.Slice(0, rb.Len()), data; got != want { |
| 80 | t.Errorf("slice mismatch: got %q, want %q", got, want) |
| 81 | } |
| 82 | if got, want := rb.Get(8), rune('r'); got != want { |
| 83 | t.Errorf("rune mismatch: got %U, want %U", got, want) |
| 84 | } |
| 85 | if _, ok := rb.(*supplementalBuffer); !ok { |
| 86 | t.Errorf("type mismatch: got %T, want %T", rb, &supplementalBuffer{}) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestNewBuffer_Empty(t *testing.T) { |
| 91 | data := "" |
| 92 | rb := NewBuffer(data) |
| 93 | if got, want := rb.Len(), utf8.RuneCountInString(data); got != want { |
| 94 | t.Errorf("length mismatch: got %d, want %d", got, want) |
| 95 | } |
| 96 | if got, want := rb.Slice(0, rb.Len()), data; got != want { |
| 97 | t.Errorf("slice mismatch: got %q, want %q", got, want) |
| 98 | } |
| 99 | if _, ok := rb.(*emptyBuffer); !ok { |
| 100 | t.Errorf("type mismatch: got %T, want %T", rb, &emptyBuffer{}) |
| 101 | } |
| 102 | } |