A simple monitoring solution written in Go (work in progress)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.6 KiB

  1. package config
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. func TestSimpleSpaces(t *testing.T) {
  7. runTest(t, "simple-spaces",
  8. `[["one","two","three","four","five","six"]]`,
  9. )
  10. }
  11. func TestSimpleMultiline(t *testing.T) {
  12. runTest(t, "simple-multiline",
  13. `[["one","two","three"],["four","five"],[],[],["six"]]`,
  14. )
  15. }
  16. func TestQuotes(t *testing.T) {
  17. runTest(t, "quotes",
  18. `[["one","two","three"],[],["four","five","six"]]`,
  19. )
  20. }
  21. func TestQuotesMultiline(t *testing.T) {
  22. runTest(t, "quotes-multiline",
  23. `[["one\ntwo"],["three\nfour"],[],[],["five\n six"]]`,
  24. )
  25. }
  26. func TestQuotesEmpty(t *testing.T) {
  27. runTest(t, "quotes-empty",
  28. `[["one","","three"],["","five",""],["seven"]]`,
  29. )
  30. }
  31. func TestComments(t *testing.T) {
  32. runTest(t, "comments",
  33. `[[],["one"],[],["two"],[],["three"]]`,
  34. )
  35. }
  36. func TestCommentsInline(t *testing.T) {
  37. runTest(t, "comments-inline",
  38. `[["one"],["two#three"],[],["four"]]`,
  39. )
  40. }
  41. func runTest(t *testing.T, bn string, exp string) {
  42. t.Logf("Running testdata/%s.tok.. ", bn)
  43. tok, err := NewTokenizer("testdata/" + bn + ".tok")
  44. if err != nil {
  45. t.Fatalf("%s", err.Error())
  46. }
  47. defer tok.Close()
  48. tokens := [][]string{}
  49. for tok.Scan() {
  50. ln := tok.Line()
  51. tl := len(tokens)
  52. if tl < ln {
  53. for i := tl; i < ln; i++ {
  54. tokens = append(tokens, []string{})
  55. }
  56. }
  57. tokens[ln-1] = append(tokens[ln-1], tok.Text())
  58. }
  59. if tok.Err() != nil {
  60. t.Fatalf("%s", tok.Err())
  61. }
  62. out, err := json.Marshal(tokens)
  63. if err != nil {
  64. t.Fatalf("%s", err)
  65. }
  66. if exp != string(out) {
  67. t.Logf("Expected: %s", exp)
  68. t.Logf("Got: %s", out)
  69. t.FailNow()
  70. }
  71. }