github.com/go-kivik/kivik/v4@v4.3.2/mockdb/gen/render_test.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package main
    14  
    15  import (
    16  	"reflect"
    17  	"testing"
    18  
    19  	"gitlab.com/flimzy/testy"
    20  )
    21  
    22  func init() {
    23  	initTemplates("templates")
    24  }
    25  
    26  func TestRenderExpectedType(t *testing.T) {
    27  	tests := testy.NewTable()
    28  	tests.Add("CreateDoc", &method{
    29  		Name:           "CreateDoc",
    30  		DBMethod:       true,
    31  		AcceptsContext: true,
    32  		AcceptsOptions: true,
    33  		ReturnsError:   true,
    34  		Accepts:        []reflect.Type{reflect.TypeOf((*interface{})(nil)).Elem()},
    35  		Returns:        []reflect.Type{typeString, typeString},
    36  	})
    37  
    38  	tests.Run(t, func(t *testing.T, m *method) {
    39  		result, err := renderExpectedType(m)
    40  		if err != nil {
    41  			t.Fatal(err)
    42  		}
    43  		if d := testy.DiffText(testy.Snapshot(t), result); d != nil {
    44  			t.Error(d)
    45  		}
    46  	})
    47  }
    48  
    49  func TestRenderDriverMethod(t *testing.T) {
    50  	type tst struct {
    51  		method *method
    52  		err    string
    53  	}
    54  	tests := testy.NewTable()
    55  	tests.Add("CreateDB", tst{
    56  		method: &method{
    57  			Name:           "CreateDB",
    58  			Accepts:        []reflect.Type{typeString},
    59  			AcceptsContext: true,
    60  			AcceptsOptions: true,
    61  			ReturnsError:   true,
    62  		},
    63  	})
    64  	tests.Add("No context", tst{
    65  		method: &method{
    66  			Name:           "NoCtx",
    67  			AcceptsOptions: true,
    68  			ReturnsError:   true,
    69  		},
    70  	})
    71  	tests.Run(t, func(t *testing.T, test tst) {
    72  		result, err := renderDriverMethod(test.method)
    73  		if !testy.ErrorMatches(test.err, err) {
    74  			t.Errorf("Unexpected error: %s", err)
    75  		}
    76  		if d := testy.DiffText(testy.Snapshot(t), result); d != nil {
    77  			t.Error(d)
    78  		}
    79  	})
    80  }
    81  
    82  func TestVariables(t *testing.T) {
    83  	type tst struct {
    84  		method   *method
    85  		indent   int
    86  		expected string
    87  	}
    88  	tests := testy.NewTable()
    89  	tests.Add("no args", tst{
    90  		method:   &method{},
    91  		expected: "",
    92  	})
    93  	tests.Add("one arg", tst{
    94  		method:   &method{Accepts: []reflect.Type{typeString}},
    95  		expected: "arg0: arg0,",
    96  	})
    97  	tests.Add("one arg + options", tst{
    98  		method: &method{Accepts: []reflect.Type{typeString}, AcceptsOptions: true},
    99  		expected: `arg0:    arg0,
   100  options: options,`,
   101  	})
   102  	tests.Add("indent", tst{
   103  		method: &method{Accepts: []reflect.Type{typeString, typeString}, AcceptsOptions: true},
   104  		indent: 2,
   105  		expected: `		arg0:    arg0,
   106  		arg1:    arg1,
   107  		options: options,`,
   108  	})
   109  
   110  	tests.Run(t, func(t *testing.T, test tst) {
   111  		result := test.method.Variables(test.indent)
   112  		if d := testy.DiffText(test.expected, result); d != nil {
   113  			t.Error(d)
   114  		}
   115  	})
   116  }