| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 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 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package pageparser |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
qt "github.com/frankban/quicktest" |
| 20 |
) |
| 21 |
|
| 22 |
func TestItemValTyped(t *testing.T) { |
| 23 |
c := qt.New(t) |
| 24 |
|
| 25 |
source := []byte("3.14") |
| 26 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, float64(3.14)) |
| 27 |
source = []byte(".14") |
| 28 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, float64(0.14)) |
| 29 |
source = []byte("314") |
| 30 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, 314) |
| 31 |
source = []byte("314") |
| 32 |
c.Assert(Item{low: 0, high: len(source), isString: true}.ValTyped(source), qt.Equals, "314") |
| 33 |
source = []byte("314x") |
| 34 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, "314x") |
| 35 |
source = []byte("314 ") |
| 36 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, "314 ") |
| 37 |
source = []byte("true") |
| 38 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, true) |
| 39 |
source = []byte("false") |
| 40 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, false) |
| 41 |
source = []byte("falsex") |
| 42 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, "falsex") |
| 43 |
source = []byte("xfalse") |
| 44 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, "xfalse") |
| 45 |
source = []byte("truex") |
| 46 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, "truex") |
| 47 |
source = []byte("xtrue") |
| 48 |
c.Assert(Item{low: 0, high: len(source)}.ValTyped(source), qt.Equals, "xtrue") |
| 49 |
} |
| 50 |
|
| 51 |
func TestItemBoolMethods(t *testing.T) { |
| 52 |
c := qt.New(t) |
| 53 |
|
| 54 |
source := []byte(" shortcode ") |
| 55 |
tests := []struct { |
| 56 |
name string |
| 57 |
item Item |
| 58 |
source []byte |
| 59 |
want bool |
| 60 |
call func(Item, []byte) bool |
| 61 |
}{ |
| 62 |
{ |
| 63 |
name: "IsText true", |
| 64 |
item: Item{Type: tText}, |
| 65 |
call: func(i Item, _ []byte) bool { return i.IsText() }, |
| 66 |
want: true, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "IsIndentation false", |
| 70 |
item: Item{Type: tText}, |
| 71 |
call: func(i Item, _ []byte) bool { return i.IsIndentation() }, |
| 72 |
want: false, |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "IsShortcodeName", |
| 76 |
item: Item{Type: tScName}, |
| 77 |
call: func(i Item, _ []byte) bool { return i.IsShortcodeName() }, |
| 78 |
want: true, |
| 79 |
}, |
| 80 |
{ |
| 81 |
name: "IsNonWhitespace true", |
| 82 |
item: Item{ |
| 83 |
Type: tText, |
| 84 |
low: 2, |
| 85 |
high: 11, |
| 86 |
}, |
| 87 |
source: source, |
| 88 |
call: func(i Item, src []byte) bool { return i.IsNonWhitespace(src) }, |
| 89 |
want: true, |
| 90 |
}, |
| 91 |
{ |
| 92 |
name: "IsShortcodeParam false", |
| 93 |
item: Item{Type: tScParamVal}, |
| 94 |
call: func(i Item, _ []byte) bool { return i.IsShortcodeParam() }, |
| 95 |
want: false, |
| 96 |
}, |
| 97 |
{ |
| 98 |
name: "IsInlineShortcodeName", |
| 99 |
item: Item{Type: tScNameInline}, |
| 100 |
call: func(i Item, _ []byte) bool { return i.IsInlineShortcodeName() }, |
| 101 |
want: true, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "IsLeftShortcodeDelim tLeftDelimScWithMarkup", |
| 105 |
item: Item{Type: tLeftDelimScWithMarkup}, |
| 106 |
call: func(i Item, _ []byte) bool { return i.IsLeftShortcodeDelim() }, |
| 107 |
want: true, |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "IsLeftShortcodeDelim tLeftDelimScNoMarkup", |
| 111 |
item: Item{Type: tLeftDelimScNoMarkup}, |
| 112 |
call: func(i Item, _ []byte) bool { return i.IsLeftShortcodeDelim() }, |
| 113 |
want: true, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "IsRightShortcodeDelim tRightDelimScWithMarkup", |
| 117 |
item: Item{Type: tRightDelimScWithMarkup}, |
| 118 |
call: func(i Item, _ []byte) bool { return i.IsRightShortcodeDelim() }, |
| 119 |
want: true, |
| 120 |
}, |
| 121 |
{ |
| 122 |
name: "IsRightShortcodeDelim tRightDelimScNoMarkup", |
| 123 |
item: Item{Type: tRightDelimScNoMarkup}, |
| 124 |
call: func(i Item, _ []byte) bool { return i.IsRightShortcodeDelim() }, |
| 125 |
want: true, |
| 126 |
}, |
| 127 |
{ |
| 128 |
name: "IsShortcodeClose", |
| 129 |
item: Item{Type: tScClose}, |
| 130 |
call: func(i Item, _ []byte) bool { return i.IsShortcodeClose() }, |
| 131 |
want: true, |
| 132 |
}, |
| 133 |
{ |
| 134 |
name: "IsShortcodeParamVal", |
| 135 |
item: Item{Type: tScParamVal}, |
| 136 |
call: func(i Item, _ []byte) bool { return i.IsShortcodeParamVal() }, |
| 137 |
want: true, |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "IsShortcodeMarkupDelimiter tLeftDelimScWithMarkup", |
| 141 |
item: Item{Type: tLeftDelimScWithMarkup}, |
| 142 |
call: func(i Item, _ []byte) bool { return i.IsShortcodeMarkupDelimiter() }, |
| 143 |
want: true, |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "IsShortcodeMarkupDelimiter tRightDelimScWithMarkup", |
| 147 |
item: Item{Type: tRightDelimScWithMarkup}, |
| 148 |
call: func(i Item, _ []byte) bool { return i.IsShortcodeMarkupDelimiter() }, |
| 149 |
want: true, |
| 150 |
}, |
| 151 |
{ |
| 152 |
name: "IsFrontMatter TypeFrontMatterYAML", |
| 153 |
item: Item{Type: TypeFrontMatterYAML}, |
| 154 |
call: func(i Item, _ []byte) bool { return i.IsFrontMatter() }, |
| 155 |
want: true, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "IsFrontMatter TypeFrontMatterTOML", |
| 159 |
item: Item{Type: TypeFrontMatterTOML}, |
| 160 |
call: func(i Item, _ []byte) bool { return i.IsFrontMatter() }, |
| 161 |
want: true, |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "IsFrontMatter TypeFrontMatterJSON", |
| 165 |
item: Item{Type: TypeFrontMatterJSON}, |
| 166 |
call: func(i Item, _ []byte) bool { return i.IsFrontMatter() }, |
| 167 |
want: true, |
| 168 |
}, |
| 169 |
{ |
| 170 |
name: "IsFrontMatter TypeFrontMatterORG", |
| 171 |
item: Item{Type: TypeFrontMatterORG}, |
| 172 |
call: func(i Item, _ []byte) bool { return i.IsFrontMatter() }, |
| 173 |
want: true, |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "IsDone tError", |
| 177 |
item: Item{Type: tError}, |
| 178 |
call: func(i Item, _ []byte) bool { return i.IsDone() }, |
| 179 |
want: true, |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "IsDone tEOF", |
| 183 |
item: Item{Type: tEOF}, |
| 184 |
call: func(i Item, _ []byte) bool { return i.IsDone() }, |
| 185 |
want: true, |
| 186 |
}, |
| 187 |
{ |
| 188 |
name: "IsEOF", |
| 189 |
item: Item{Type: tEOF}, |
| 190 |
call: func(i Item, _ []byte) bool { return i.IsEOF() }, |
| 191 |
want: true, |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: "IsError", |
| 195 |
item: Item{Type: tError}, |
| 196 |
call: func(i Item, _ []byte) bool { return i.IsError() }, |
| 197 |
want: true, |
| 198 |
}, |
| 199 |
} |
| 200 |
|
| 201 |
for _, tt := range tests { |
| 202 |
t.Run(tt.name, func(t *testing.T) { |
| 203 |
got := tt.call(tt.item, tt.source) |
| 204 |
c.Assert(got, qt.Equals, tt.want) |
| 205 |
}) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func TestItem_ToString(t *testing.T) { |
| 210 |
c := qt.New(t) |
| 211 |
|
| 212 |
source := []byte("src") |
| 213 |
long := make([]byte, 100) |
| 214 |
for i := range long { |
| 215 |
long[i] = byte(i) |
| 216 |
} |
| 217 |
|
| 218 |
tests := []struct { |
| 219 |
name string |
| 220 |
item Item |
| 221 |
source []byte |
| 222 |
want string |
| 223 |
call func(Item, []byte) string |
| 224 |
}{ |
| 225 |
{ |
| 226 |
name: "EOF", |
| 227 |
item: Item{Type: tEOF}, |
| 228 |
call: func(i Item, _ []byte) string { return i.ToString(source) }, |
| 229 |
want: "EOF", |
| 230 |
}, |
| 231 |
{ |
| 232 |
name: "Error", |
| 233 |
item: Item{Type: tError}, |
| 234 |
call: func(i Item, _ []byte) string { return i.ToString(source) }, |
| 235 |
want: "", |
| 236 |
}, |
| 237 |
{ |
| 238 |
name: "Indentation", |
| 239 |
item: Item{Type: tIndentation}, |
| 240 |
call: func(i Item, _ []byte) string { return i.ToString(source) }, |
| 241 |
want: "tIndentation:[]", |
| 242 |
}, |
| 243 |
{ |
| 244 |
name: "Long", |
| 245 |
item: Item{Type: tKeywordMarker + 1, low: 0, high: 100}, |
| 246 |
call: func(i Item, _ []byte) string { return i.ToString(long) }, |
| 247 |
want: "<" + string(long) + ">", |
| 248 |
}, |
| 249 |
{ |
| 250 |
name: "Empty", |
| 251 |
item: Item{Type: tKeywordMarker + 1}, |
| 252 |
call: func(i Item, _ []byte) string { return i.ToString([]byte("")) }, |
| 253 |
want: "<>", |
| 254 |
}, |
| 255 |
} |
| 256 |
|
| 257 |
for _, tt := range tests { |
| 258 |
t.Run(tt.name, func(t *testing.T) { |
| 259 |
got := tt.call(tt.item, tt.source) |
| 260 |
c.Assert(got, qt.Equals, tt.want) |
| 261 |
}) |
| 262 |
} |
| 263 |
} |