github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/jsonconfig/jsonconfig_test.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package jsonconfig
    18  
    19  import (
    20  	"os"
    21  	"reflect"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestIncludes(t *testing.T) {
    27  	obj, err := ReadFile("testdata/include1.json")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	two := obj.RequiredObject("two")
    32  	if err := obj.Validate(); err != nil {
    33  		t.Error(err)
    34  	}
    35  	if g, e := two.RequiredString("key"), "value"; g != e {
    36  		t.Errorf("sub object key = %q; want %q", g, e)
    37  	}
    38  }
    39  
    40  func TestIncludeLoop(t *testing.T) {
    41  	_, err := ReadFile("testdata/loop1.json")
    42  	if err == nil {
    43  		t.Fatal("expected an error about import cycles.")
    44  	}
    45  	if !strings.Contains(err.Error(), "include cycle detected") {
    46  		t.Fatal("expected an error about import cycles; got: %v", err)
    47  	}
    48  }
    49  
    50  func TestBoolEnvs(t *testing.T) {
    51  	os.Setenv("TEST_EMPTY", "")
    52  	os.Setenv("TEST_TRUE", "true")
    53  	os.Setenv("TEST_ONE", "1")
    54  	os.Setenv("TEST_ZERO", "0")
    55  	os.Setenv("TEST_FALSE", "false")
    56  	obj, err := ReadFile("testdata/boolenv.json")
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if str := obj.RequiredString("emptystr"); str != "" {
    61  		t.Errorf("str = %q, want empty", str)
    62  	}
    63  	tests := []struct {
    64  		key  string
    65  		want bool
    66  	}{
    67  		{"def_false", false},
    68  		{"def_true", true},
    69  		{"set_true_def_false", true},
    70  		{"set_false_def_true", false},
    71  		{"lit_true", true},
    72  		{"lit_false", false},
    73  		{"one", true},
    74  		{"zero", false},
    75  	}
    76  	for _, tt := range tests {
    77  		if v := obj.RequiredBool(tt.key); v != tt.want {
    78  			t.Errorf("key %q = %v; want %v", tt.key, v, tt.want)
    79  		}
    80  	}
    81  	if err := obj.Validate(); err != nil {
    82  		t.Error(err)
    83  	}
    84  }
    85  
    86  func TestListExpansion(t *testing.T) {
    87  	os.Setenv("TEST_BAR", "bar")
    88  	obj, err := ReadFile("testdata/listexpand.json")
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	s := obj.RequiredString("str")
    93  	l := obj.RequiredList("list")
    94  	if err := obj.Validate(); err != nil {
    95  		t.Error(err)
    96  	}
    97  	want := []string{"foo", "bar"}
    98  	if !reflect.DeepEqual(l, want) {
    99  		t.Errorf("got = %#v\nwant = %#v", l, want)
   100  	}
   101  	if s != "bar" {
   102  		t.Errorf("str = %q, want %q", s, "bar")
   103  	}
   104  }