dict_test.go 955 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package template
  5. import (
  6. "testing"
  7. )
  8. func TestDict(t *testing.T) {
  9. d, err := dict("k1", "v1", "k2", "v2")
  10. if err != nil {
  11. t.Fatalf(`The dict should be valid: %v`, err)
  12. }
  13. if value, found := d["k1"]; found {
  14. if value != "v1" {
  15. t.Fatalf(`Incorrect value for k1: %q`, value)
  16. }
  17. }
  18. if value, found := d["k2"]; found {
  19. if value != "v2" {
  20. t.Fatalf(`Incorrect value for k2: %q`, value)
  21. }
  22. }
  23. }
  24. func TestDictWithIncorrectNumberOfPairs(t *testing.T) {
  25. _, err := dict("k1", "v1", "k2")
  26. if err == nil {
  27. t.Fatalf(`The dict should not be valid because the number of keys/values pairs are incorrect`)
  28. }
  29. }
  30. func TestDictWithInvalidKey(t *testing.T) {
  31. _, err := dict(1, "v1")
  32. if err == nil {
  33. t.Fatalf(`The dict should not be valid because the key is not a string`)
  34. }
  35. }