github.com/mithrandie/csvq@v1.18.1/lib/option/flags_test.go (about) 1 package option 2 3 import ( 4 "path/filepath" 5 "reflect" 6 "runtime" 7 "testing" 8 "time" 9 10 "github.com/mithrandie/go-text" 11 "github.com/mithrandie/go-text/json" 12 ) 13 14 func TestImportOptions_Copy(t *testing.T) { 15 op := NewImportOptions() 16 op.DelimiterPositions = []int{1, 2, 3} 17 18 expect := NewImportOptions() 19 expect.DelimiterPositions = []int{1, 2, 3} 20 21 copied := op.Copy() 22 if !reflect.DeepEqual(copied, expect) { 23 t.Errorf("import options = %v, want %v", copied, expect) 24 } 25 } 26 27 func TestExportOptions_Copy(t *testing.T) { 28 op := NewExportOptions() 29 op.DelimiterPositions = []int{1, 2, 3} 30 31 expect := NewExportOptions() 32 expect.DelimiterPositions = []int{1, 2, 3} 33 34 copied := op.Copy() 35 if !reflect.DeepEqual(copied, expect) { 36 t.Errorf("export options = %v, want %v", copied, expect) 37 } 38 } 39 40 func TestFlags_GetTimeLocation(t *testing.T) { 41 flags, _ := NewFlags(nil) 42 43 local, _ := time.LoadLocation("Local") 44 loc := flags.GetTimeLocation() 45 if local != loc { 46 t.Errorf("location = %q, want %q", loc.String(), local.String()) 47 } 48 49 _ = flags.SetLocation("UTC") 50 utc, _ := time.LoadLocation("UTC") 51 loc = flags.GetTimeLocation() 52 if utc != loc { 53 t.Errorf("location = %q, want %q", loc.String(), utc.String()) 54 } 55 } 56 57 func TestFlags_SetRepository(t *testing.T) { 58 flags, _ := NewFlags(nil) 59 60 _ = flags.SetRepository("") 61 if flags.Repository != "" { 62 t.Errorf("repository = %s, expect to set %q for %q", flags.Repository, "", "") 63 } 64 65 dir := filepath.Join("..", "..", "lib", "option") 66 absdir, _ := filepath.Abs(dir) 67 _ = flags.SetRepository(dir) 68 if flags.Repository != absdir { 69 t.Errorf("repository = %s, expect to set %s for %s", flags.Repository, absdir, dir) 70 } 71 72 expectErr := "repository does not exist" 73 err := flags.SetRepository("notexists") 74 if err == nil { 75 t.Errorf("no error, want error %q for %s", expectErr, "error") 76 } else if err.Error() != expectErr { 77 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "notexists") 78 } 79 80 expectErr = "repository must be a directory path" 81 err = flags.SetRepository("flags_test.go") 82 if err == nil { 83 t.Errorf("no error, want error %q for %s", expectErr, "error") 84 } else if err.Error() != expectErr { 85 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "flags_test.go") 86 } 87 } 88 89 func TestFlags_SetLocation(t *testing.T) { 90 flags, _ := NewFlags(nil) 91 92 s := "" 93 _ = flags.SetLocation(s) 94 if flags.Location != "Local" { 95 t.Errorf("location = %s, expect to set %s for %q", flags.Location, "Local", "") 96 } 97 98 s = "local" 99 _ = flags.SetLocation(s) 100 if flags.Location != "Local" { 101 t.Errorf("location = %s, expect to set %s for %q", flags.Location, "Local", s) 102 } 103 104 s = "utc" 105 _ = flags.SetLocation(s) 106 if flags.Location != "UTC" { 107 t.Errorf("location = %s, expect to set %s for %q", flags.Location, "UTC", s) 108 } 109 110 s = "America/NotExist" 111 expectErr := "timezone \"America/NotExist\" does not exist" 112 err := flags.SetLocation(s) 113 if err == nil { 114 t.Errorf("no error, want error %q for %s", expectErr, s) 115 } else if err.Error() != expectErr { 116 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, s) 117 } 118 } 119 120 func TestFlags_SetDatetimeFormat(t *testing.T) { 121 flags, _ := NewFlags(nil) 122 123 format := "%Y-%m-%d" 124 flags.SetDatetimeFormat(format) 125 expect := []string{ 126 "%Y-%m-%d", 127 } 128 if !reflect.DeepEqual(flags.DatetimeFormat, expect) { 129 t.Errorf("datetime format = %s, expect to set %s", flags.DatetimeFormat, expect) 130 } 131 132 format = "" 133 flags.SetDatetimeFormat(format) 134 expect = []string{ 135 "%Y-%m-%d", 136 } 137 if !reflect.DeepEqual(flags.DatetimeFormat, expect) { 138 t.Errorf("datetime format = %s, expect to set %s", flags.DatetimeFormat, expect) 139 } 140 141 format = "[\"%Y-%m-%d %H:%i:%s\"]" 142 flags.SetDatetimeFormat(format) 143 expect = []string{ 144 "%Y-%m-%d", 145 "%Y-%m-%d %H:%i:%s", 146 } 147 if !reflect.DeepEqual(flags.DatetimeFormat, expect) { 148 t.Errorf("datetime format = %s, expect to set %s", flags.DatetimeFormat, expect) 149 } 150 } 151 152 func TestFlags_SetAnsiQuotes(t *testing.T) { 153 flags, _ := NewFlags(nil) 154 155 flags.SetAnsiQuotes(true) 156 if !flags.AnsiQuotes { 157 t.Errorf("ansi_quotes = %t, expect to set %t", flags.AnsiQuotes, true) 158 } 159 } 160 161 func TestFlags_SetStrictEqual(t *testing.T) { 162 flags, _ := NewFlags(nil) 163 164 flags.SetStrictEqual(true) 165 if !flags.StrictEqual { 166 t.Errorf("strict_equal = %t, expect to set %t", flags.StrictEqual, true) 167 } 168 } 169 170 func TestFlags_SetWaitTimeout(t *testing.T) { 171 flags, _ := NewFlags(nil) 172 173 var f float64 = -1 174 flags.SetWaitTimeout(f) 175 if flags.WaitTimeout != 0 { 176 t.Errorf("wait timeout = %f, expect to set %f for %f", flags.WaitTimeout, 0.0, f) 177 } 178 179 f = 15 180 flags.SetWaitTimeout(f) 181 if flags.WaitTimeout != 15 { 182 t.Errorf("wait timeout = %f, expect to set %f for %f", flags.WaitTimeout, 15.0, f) 183 } 184 } 185 186 func TestFlags_SetImportFormat(t *testing.T) { 187 flags, _ := NewFlags(nil) 188 189 _ = flags.SetImportFormat("") 190 if flags.ImportOptions.Format != CSV { 191 t.Errorf("importFormat = %s, expect to set %s for empty string", flags.ImportOptions.Format, CSV) 192 } 193 194 _ = flags.SetImportFormat("json") 195 if flags.ImportOptions.Format != JSON { 196 t.Errorf("importFormat = %s, expect to set %s for empty string", flags.ImportOptions.Format, JSON) 197 } 198 199 expectErr := "import format must be one of CSV|TSV|FIXED|JSON|JSONL|LTSV" 200 err := flags.SetImportFormat("error") 201 if err == nil { 202 t.Errorf("no error, want error %q for %s", expectErr, "error") 203 } else if err.Error() != expectErr { 204 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "error") 205 } 206 207 err = flags.SetImportFormat("text") 208 if err == nil { 209 t.Errorf("no error, want error %q for %s", expectErr, "error") 210 } else if err.Error() != expectErr { 211 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "error") 212 } 213 } 214 215 func TestFlags_SetDelimiter(t *testing.T) { 216 flags, _ := NewFlags(nil) 217 218 _ = flags.SetDelimiter("") 219 if flags.ImportOptions.Delimiter != ',' { 220 t.Errorf("delimiter = %q, expect to set %q for %q", flags.ImportOptions.Delimiter, ',', "") 221 } 222 223 _ = flags.SetDelimiter("\\t") 224 if flags.ImportOptions.Delimiter != '\t' { 225 t.Errorf("delimiter = %q, expect to set %q for %q", flags.ImportOptions.Delimiter, "\t", "\t") 226 } 227 228 expectErr := "delimiter must be one character" 229 err := flags.SetDelimiter("[a]") 230 if err == nil { 231 t.Errorf("no error, want error %q for %s", expectErr, "//") 232 } else if err.Error() != expectErr { 233 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "//") 234 } 235 236 expectErr = "delimiter must be one character" 237 err = flags.SetDelimiter("//") 238 if err == nil { 239 t.Errorf("no error, want error %q for %s", expectErr, "//") 240 } else if err.Error() != expectErr { 241 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "//") 242 } 243 } 244 245 func TestFlags_SetDelimiterPositions(t *testing.T) { 246 flags, _ := NewFlags(nil) 247 248 _ = flags.SetDelimiterPositions("") 249 if flags.ImportOptions.DelimiterPositions != nil { 250 t.Errorf("delimiter-positions = %v, expect to set %v for %q", flags.ImportOptions.DelimiterPositions, nil, "") 251 } 252 253 _ = flags.SetDelimiterPositions("s[1, 2, 3]") 254 if flags.ImportOptions.SingleLine != true { 255 t.Errorf("singleLine = %t, expect to set %t for %q", flags.ImportOptions.SingleLine, true, "s[1, 2, 3]") 256 } 257 if !reflect.DeepEqual(flags.ImportOptions.DelimiterPositions, []int{1, 2, 3}) { 258 t.Errorf("delimitPositions = %v, expect to set %v for %q", flags.ImportOptions.DelimiterPositions, []int{1, 2, 3}, "[1, 2, 3]") 259 } 260 261 _ = flags.SetDelimiterPositions("[1, 2, 3]") 262 if flags.ImportOptions.SingleLine != false { 263 t.Errorf("singleLine = %t, expect to set %t for %q", flags.ImportOptions.SingleLine, false, "[1, 2, 3]") 264 } 265 if !reflect.DeepEqual(flags.ImportOptions.DelimiterPositions, []int{1, 2, 3}) { 266 t.Errorf("delimitPositions = %v, expect to set %v for %q", flags.ImportOptions.DelimiterPositions, []int{1, 2, 3}, "[1, 2, 3]") 267 } 268 269 _ = flags.SetDelimiterPositions("spaces") 270 if flags.ImportOptions.SingleLine != false { 271 t.Errorf("singleLine = %t, expect to set %t for %q", flags.ImportOptions.SingleLine, false, "spaces") 272 } 273 if flags.ImportOptions.DelimiterPositions != nil { 274 t.Errorf("delimitPositions = %v, expect to set %v for %q", flags.ImportOptions.DelimiterPositions, nil, "spaces") 275 } 276 277 expectErr := "delimiter positions must be \"SPACES\" or a JSON array of integers" 278 err := flags.SetDelimiterPositions("[a]") 279 if err == nil { 280 t.Errorf("no error, want error %q for %s", expectErr, "//") 281 } else if err.Error() != expectErr { 282 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "//") 283 } 284 285 err = flags.SetDelimiterPositions("//") 286 if err == nil { 287 t.Errorf("no error, want error %q for %s", expectErr, "//") 288 } else if err.Error() != expectErr { 289 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "//") 290 } 291 } 292 293 func TestFlags_SetJsonQuery(t *testing.T) { 294 flags, _ := NewFlags(nil) 295 296 flags.SetJsonQuery("{}") 297 if flags.ImportOptions.JsonQuery != "{}" { 298 t.Errorf("json-query = %q, expect to set %q", flags.ImportOptions.JsonQuery, "{}") 299 } 300 } 301 302 func TestFlags_SetEncoding(t *testing.T) { 303 flags, _ := NewFlags(nil) 304 305 _ = flags.SetEncoding("sjis") 306 if flags.ImportOptions.Encoding != text.SJIS { 307 t.Errorf("encoding = %s, expect to set %s for %s", flags.ImportOptions.Encoding, text.SJIS, "sjis") 308 } 309 310 expectErr := "encoding must be one of AUTO|UTF8|UTF8M|UTF16|UTF16BE|UTF16LE|UTF16BEM|UTF16LEM|SJIS" 311 err := flags.SetEncoding("error") 312 if err == nil { 313 t.Errorf("no error, want error %q for %s", expectErr, "error") 314 } else if err.Error() != expectErr { 315 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "error") 316 } 317 } 318 319 func TestFlags_SetNoHeader(t *testing.T) { 320 flags, _ := NewFlags(nil) 321 322 flags.SetNoHeader(true) 323 if !flags.ImportOptions.NoHeader { 324 t.Errorf("no-header = %t, expect to set %t", flags.ImportOptions.NoHeader, true) 325 } 326 } 327 328 func TestFlags_SetWithoutNull(t *testing.T) { 329 flags, _ := NewFlags(nil) 330 331 flags.SetWithoutNull(true) 332 if !flags.ImportOptions.WithoutNull { 333 t.Errorf("without-null = %t, expect to set %t", flags.ImportOptions.WithoutNull, true) 334 } 335 } 336 337 func TestFlags_SetFormat(t *testing.T) { 338 flags, _ := NewFlags(nil) 339 340 _ = flags.SetFormat("", "", false) 341 if flags.ExportOptions.Format != TEXT { 342 t.Errorf("format = %s, expect to set %s for empty string", flags.ExportOptions.Format, TEXT) 343 } 344 345 _ = flags.SetFormat("", "", true) 346 if flags.ExportOptions.Format != CSV { 347 t.Errorf("format = %s, expect to set %s for empty string", flags.ExportOptions.Format, CSV) 348 } 349 350 _ = flags.SetFormat("", "foo", true) 351 if flags.ExportOptions.Format != TEXT { 352 t.Errorf("format = %s, expect to set %s for empty string with file %q", flags.ExportOptions.Format, TEXT, "foo") 353 } 354 355 _ = flags.SetFormat("", "foo.csv", false) 356 if flags.ExportOptions.Format != CSV { 357 t.Errorf("format = %s, expect to set %s for empty string with file %q", flags.ExportOptions.Format, CSV, "foo.csv") 358 } 359 360 _ = flags.SetFormat("", "foo.tsv", false) 361 if flags.ExportOptions.Format != TSV { 362 t.Errorf("format = %s, expect to set %s for empty string with file %q", flags.ExportOptions.Format, TSV, "foo.tsv") 363 } 364 365 _ = flags.SetFormat("", "foo.json", false) 366 if flags.ExportOptions.Format != JSON { 367 t.Errorf("format = %s, expect to set %s for empty string with file %q", flags.ExportOptions.Format, JSON, "foo.json") 368 } 369 370 _ = flags.SetFormat("", "foo.jsonl", false) 371 if flags.ExportOptions.Format != JSONL { 372 t.Errorf("format = %s, expect to set %s for empty string with file %q", flags.ExportOptions.Format, JSONL, "foo.jsonl") 373 } 374 375 _ = flags.SetFormat("", "foo.ltsv", false) 376 if flags.ExportOptions.Format != LTSV { 377 t.Errorf("format = %s, expect to set %s for empty string with file %q", flags.ExportOptions.Format, LTSV, "foo.ltsv") 378 } 379 380 _ = flags.SetFormat("", "foo.md", false) 381 if flags.ExportOptions.Format != GFM { 382 t.Errorf("format = %s, expect to set %s for empty string with file %q", flags.ExportOptions.Format, GFM, "foo.md") 383 } 384 385 _ = flags.SetFormat("", "foo.org", false) 386 if flags.ExportOptions.Format != ORG { 387 t.Errorf("format = %s, expect to set %s for empty string with file %q", flags.ExportOptions.Format, ORG, "foo.org") 388 } 389 390 _ = flags.SetFormat("csv", "", false) 391 if flags.ExportOptions.Format != CSV { 392 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, CSV, "csv") 393 } 394 395 _ = flags.SetFormat("tsv", "", false) 396 if flags.ExportOptions.Format != TSV { 397 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, TSV, "tsv") 398 } 399 400 _ = flags.SetFormat("fixed", "", false) 401 if flags.ExportOptions.Format != FIXED { 402 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, FIXED, "fixed") 403 } 404 405 _ = flags.SetFormat("json", "", false) 406 if flags.ExportOptions.Format != JSON { 407 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, JSON, "json") 408 } 409 410 _ = flags.SetFormat("jsonl", "", false) 411 if flags.ExportOptions.Format != JSONL { 412 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, JSONL, "jsonl") 413 } 414 415 _ = flags.SetFormat("ltsv", "", false) 416 if flags.ExportOptions.Format != LTSV { 417 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, LTSV, "ltsv") 418 } 419 420 _ = flags.SetFormat("jsonh", "", false) 421 if flags.ExportOptions.Format != JSON { 422 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, JSON, "jsonh") 423 } 424 if flags.ExportOptions.JsonEscape != json.HexDigits { 425 t.Errorf("json escape type = %v, expect to set %v for %s", flags.ExportOptions.JsonEscape, json.HexDigits, "jsonh") 426 } 427 428 _ = flags.SetFormat("jsona", "", false) 429 if flags.ExportOptions.Format != JSON { 430 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, JSON, "jsona") 431 } 432 if flags.ExportOptions.JsonEscape != json.AllWithHexDigits { 433 t.Errorf("json escape type = %v, expect to set %v for %s", flags.ExportOptions.JsonEscape, json.AllWithHexDigits, "jsonh") 434 } 435 436 _ = flags.SetFormat("gfm", "", false) 437 if flags.ExportOptions.Format != GFM { 438 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, GFM, "gfm") 439 } 440 441 _ = flags.SetFormat("org", "", false) 442 if flags.ExportOptions.Format != ORG { 443 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, ORG, "org") 444 } 445 446 _ = flags.SetFormat("box", "", false) 447 if flags.ExportOptions.Format != BOX { 448 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, BOX, "box") 449 } 450 451 _ = flags.SetFormat("text", "", false) 452 if flags.ExportOptions.Format != TEXT { 453 t.Errorf("format = %s, expect to set %s for %s", flags.ExportOptions.Format, TEXT, "text") 454 } 455 456 expectErr := "format must be one of CSV|TSV|FIXED|JSON|JSONL|LTSV|GFM|ORG|BOX|TEXT" 457 err := flags.SetFormat("error", "", false) 458 if err == nil { 459 t.Errorf("no error, want error %q for %s", expectErr, "error") 460 } else if err.Error() != expectErr { 461 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "error") 462 } 463 } 464 465 func TestFlags_SetWriteEncoding(t *testing.T) { 466 flags, _ := NewFlags(nil) 467 468 _ = flags.SetWriteEncoding("sjis") 469 if flags.ExportOptions.Encoding != text.SJIS { 470 t.Errorf("encoding = %s, expect to set %s for %s", flags.ExportOptions.Encoding, text.SJIS, "sjis") 471 } 472 473 expectErr := "write-encoding must be one of UTF8|UTF8M|UTF16|UTF16BE|UTF16LE|UTF16BEM|UTF16LEM|SJIS" 474 err := flags.SetWriteEncoding("error") 475 if err == nil { 476 t.Errorf("no error, want error %q for %s", expectErr, "error") 477 } else if err.Error() != expectErr { 478 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "error") 479 } 480 } 481 482 func TestFlags_SetWriteDelimiter(t *testing.T) { 483 flags, _ := NewFlags(nil) 484 485 _ = flags.SetWriteDelimiter("") 486 if flags.ExportOptions.Delimiter != ',' { 487 t.Errorf("write-delimiter = %q, expect to set %q for %q, format = %s", flags.ExportOptions.Delimiter, ',', "", flags.ExportOptions.Format) 488 } 489 490 _ = flags.SetWriteDelimiter("\\t") 491 if flags.ExportOptions.Delimiter != '\t' { 492 t.Errorf("write-delimiter = %q, expect to set %q for %q", flags.ExportOptions.Delimiter, "\t", "\t") 493 } 494 495 expectErr := "write-delimiter must be one character" 496 err := flags.SetWriteDelimiter("//") 497 if err == nil { 498 t.Errorf("no error, want error %q for %s", expectErr, "//") 499 } else if err.Error() != expectErr { 500 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "//") 501 } 502 } 503 504 func TestFlags_SetWriteDelimiterPositions(t *testing.T) { 505 flags, _ := NewFlags(nil) 506 507 _ = flags.SetWriteDelimiterPositions("s[1, 2, 3]") 508 if flags.ExportOptions.SingleLine != true { 509 t.Errorf("WriteAsSingleLine = %t, expect to set %t for %q", flags.ExportOptions.SingleLine, true, "s[1, 2, 3]") 510 } 511 if !reflect.DeepEqual(flags.ExportOptions.DelimiterPositions, []int{1, 2, 3}) { 512 t.Errorf("WriteDelimiterPositions = %v, expect to set %v for %q", flags.ExportOptions.DelimiterPositions, []int{1, 2, 3}, "s[1, 2, 3]") 513 } 514 515 _ = flags.SetWriteDelimiterPositions("[1, 2, 3]") 516 if flags.ExportOptions.SingleLine != false { 517 t.Errorf("WriteAsSingleLine = %t, expect to set %t for %q", flags.ExportOptions.SingleLine, false, "[1, 2, 3]") 518 } 519 if !reflect.DeepEqual(flags.ExportOptions.DelimiterPositions, []int{1, 2, 3}) { 520 t.Errorf("WriteDelimiterPositions = %v, expect to set %v for %q", flags.ExportOptions.DelimiterPositions, []int{1, 2, 3}, "[1, 2, 3]") 521 } 522 523 expectErr := "write-delimiter-positions must be \"SPACES\" or a JSON array of integers" 524 err := flags.SetWriteDelimiterPositions("//") 525 if err == nil { 526 t.Errorf("no error, want error %q for %s", expectErr, "//") 527 } else if err.Error() != expectErr { 528 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "//") 529 } 530 } 531 532 func TestFlags_SetWithoutHeader(t *testing.T) { 533 flags, _ := NewFlags(nil) 534 535 flags.SetWithoutHeader(true) 536 if !flags.ExportOptions.WithoutHeader { 537 t.Errorf("without-header = %t, expect to set %t", flags.ExportOptions.WithoutHeader, true) 538 } 539 } 540 541 func TestFlags_SetLineBreak(t *testing.T) { 542 flags, _ := NewFlags(nil) 543 544 _ = flags.SetLineBreak("") 545 if flags.ExportOptions.LineBreak != text.LF { 546 t.Errorf("line-break = %s, expect to set %s for %q", flags.ExportOptions.LineBreak, text.LF, "") 547 } 548 549 _ = flags.SetLineBreak("crlf") 550 if flags.ExportOptions.LineBreak != text.CRLF { 551 t.Errorf("line-break = %s, expect to set %s for %s", flags.ExportOptions.LineBreak, text.CRLF, "crlf") 552 } 553 554 _ = flags.SetLineBreak("cr") 555 if flags.ExportOptions.LineBreak != text.CR { 556 t.Errorf("line-break = %s, expect to set %s for %s", flags.ExportOptions.LineBreak, text.CR, "cr") 557 } 558 559 _ = flags.SetLineBreak("lf") 560 if flags.ExportOptions.LineBreak != text.LF { 561 t.Errorf("line-break = %s, expect to set %s for %s", flags.ExportOptions.LineBreak, text.LF, "LF") 562 } 563 564 expectErr := "line-break must be one of CRLF|CR|LF" 565 err := flags.SetLineBreak("error") 566 if err == nil { 567 t.Errorf("no error, want error %q for %s", expectErr, "error") 568 } else if err.Error() != expectErr { 569 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, "error") 570 } 571 } 572 573 func TestFlags_SetEncloseAll(t *testing.T) { 574 flags, _ := NewFlags(nil) 575 576 flags.SetEncloseAll(true) 577 if !flags.ExportOptions.EncloseAll { 578 t.Errorf("enclose-all = %t, expect to set %t", flags.ExportOptions.EncloseAll, true) 579 } 580 } 581 582 func TestFlags_SetJsonEscape(t *testing.T) { 583 flags, _ := NewFlags(nil) 584 585 s := "backslash" 586 _ = flags.SetJsonEscape(s) 587 if flags.ExportOptions.JsonEscape != json.Backslash { 588 t.Errorf("json-escape = %v, expect to set %v", flags.ExportOptions.JsonEscape, json.Backslash) 589 } 590 591 s = "hex" 592 _ = flags.SetJsonEscape(s) 593 if flags.ExportOptions.JsonEscape != json.HexDigits { 594 t.Errorf("json-escape = %v, expect to set %v", flags.ExportOptions.JsonEscape, json.HexDigits) 595 } 596 597 s = "hexall" 598 _ = flags.SetJsonEscape(s) 599 if flags.ExportOptions.JsonEscape != json.AllWithHexDigits { 600 t.Errorf("json-escape = %v, expect to set %v", flags.ExportOptions.JsonEscape, json.AllWithHexDigits) 601 } 602 603 s = "error" 604 expectErr := "json escape type must be one of BACKSLASH|HEX|HEXALL" 605 err := flags.SetJsonEscape(s) 606 if err == nil { 607 t.Errorf("no error, want error %q for %s", expectErr, s) 608 } else if err.Error() != expectErr { 609 t.Errorf("error = %q, want error %q for %s", err.Error(), expectErr, s) 610 } 611 } 612 613 func TestFlags_SetPrettyPrint(t *testing.T) { 614 flags, _ := NewFlags(nil) 615 616 flags.SetPrettyPrint(true) 617 if !flags.ExportOptions.PrettyPrint { 618 t.Errorf("pretty-print = %t, expect to set %t", flags.ExportOptions.PrettyPrint, true) 619 } 620 } 621 622 func TestFlags_SetScientificNotation(t *testing.T) { 623 flags, _ := NewFlags(nil) 624 625 flags.SetScientificNotation(true) 626 if !flags.ExportOptions.ScientificNotation { 627 t.Errorf("scientific-notation = %t, expect to set %t", flags.ExportOptions.ScientificNotation, true) 628 } 629 } 630 631 func TestFlags_SetStripEndingLineBreak(t *testing.T) { 632 flags, _ := NewFlags(nil) 633 634 flags.SetStripEndingLineBreak(true) 635 if !flags.ExportOptions.StripEndingLineBreak { 636 t.Errorf("strip ending line break = %t, expect to set %t", flags.ExportOptions.StripEndingLineBreak, true) 637 } 638 flags.SetStripEndingLineBreak(false) 639 } 640 641 func TestFlags_SetEastAsianEncoding(t *testing.T) { 642 flags, _ := NewFlags(nil) 643 644 flags.SetEastAsianEncoding(true) 645 if !flags.ExportOptions.EastAsianEncoding { 646 t.Errorf("east-asian-encoding = %t, expect to set %t", flags.ExportOptions.EastAsianEncoding, true) 647 } 648 } 649 650 func TestFlags_SetCountDiacriticalSign(t *testing.T) { 651 flags, _ := NewFlags(nil) 652 653 flags.SetCountDiacriticalSign(true) 654 if !flags.ExportOptions.CountDiacriticalSign { 655 t.Errorf("count-diacritical-sign = %t, expect to set %t", flags.ExportOptions.CountDiacriticalSign, true) 656 } 657 } 658 659 func TestFlags_SetCountFormatCode(t *testing.T) { 660 flags, _ := NewFlags(nil) 661 662 flags.SetCountFormatCode(true) 663 if !flags.ExportOptions.CountFormatCode { 664 t.Errorf("count-format-code = %t, expect to set %t", flags.ExportOptions.CountFormatCode, true) 665 } 666 } 667 668 func TestFlags_SetColor(t *testing.T) { 669 flags, _ := NewFlags(nil) 670 671 flags.SetColor(true) 672 if !flags.ExportOptions.Color { 673 t.Errorf("color = %t, expect to set %t", flags.ExportOptions.Color, true) 674 } 675 flags.SetColor(false) 676 } 677 678 func TestFlags_SetQuiet(t *testing.T) { 679 flags, _ := NewFlags(nil) 680 681 flags.SetQuiet(true) 682 if !flags.Quiet { 683 t.Errorf("quiet = %t, expect to set %t", flags.Quiet, true) 684 } 685 } 686 687 func TestFlags_SetLimitRecursion(t *testing.T) { 688 flags, _ := NewFlags(nil) 689 690 flags.SetLimitRecursion(int64(-100)) 691 if flags.LimitRecursion != -1 { 692 t.Errorf("limit_recursion = %d, expect to set %d", flags.LimitRecursion, -100) 693 } 694 695 flags.SetLimitRecursion(int64(10000)) 696 if flags.LimitRecursion != 10000 { 697 t.Errorf("limit_recursion = %d, expect to set %d", flags.LimitRecursion, 10000) 698 } 699 } 700 701 func TestFlags_SetCPU(t *testing.T) { 702 flags, _ := NewFlags(nil) 703 704 flags.SetCPU(0) 705 expect := 1 706 if expect != flags.CPU { 707 t.Errorf("cpu = %d, expect to set %d", flags.CPU, 1) 708 } 709 710 flags.SetCPU(runtime.NumCPU() + 100) 711 if runtime.NumCPU() != flags.CPU { 712 t.Errorf("cpu = %d, expect to set %d", flags.CPU, runtime.NumCPU()) 713 } 714 } 715 716 func TestFlags_SetStats(t *testing.T) { 717 flags, _ := NewFlags(nil) 718 719 flags.SetStats(true) 720 if !flags.Stats { 721 t.Errorf("stats = %t, expect to set %t", flags.Stats, true) 722 } 723 }