Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for default-gen
Browse files Browse the repository at this point in the history
Chao Xu committed May 17, 2017
1 parent af11bb5 commit e0660ff
Showing 5 changed files with 141 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/defaulter-gen/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
TOOL=defaulter-gen

test:
@if ! git diff --quiet HEAD; then \
echo "FAIL: git client is not clean"; \
false; \
fi
@go build -o /tmp/$(TOOL)
/tmp/$(TOOL) --logtostderr --v=6 -i ./output_tests/wholepkg -O zz_generated
@if ! diff -q ./output_tests/wholepkg/zz_generated.expected ./output_tests/wholepkg/zz_generated.go; then \
echo "FAIL: output files changed"; \
diff ./output_tests/wholepkg/zz_generated.expected ./output_tests/wholepkg/zz_generated.go; \
false; \
fi
rm ./output_tests/wholepkg/zz_generated.go
40 changes: 40 additions & 0 deletions examples/defaulter-gen/output_tests/wholepkg/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package wholepkg

type ManualDefault struct{}

func SetDefaults_ManualDefault(obj *ManualDefault) {
}

type Struct_With_Field_Has_Manual_Defaulter struct {
TypeMeta struct{}
ManualDefault ManualDefault
OtherField string
}

type Struct_With_Field_With_Field_Has_Manual_Defaulter struct {
TypeMeta struct{}
Struct_With_Field_Has_Manual_Defaulter
OtherField string
}

// No defaulter will be generated
type Struct_No_Field_Has_Manual_Defaulter struct {
TypeMeta struct{}
OtherField string
}
20 changes: 20 additions & 0 deletions examples/defaulter-gen/output_tests/wholepkg/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package wholepkg

// Another type in another file.
type Struct_B struct{}
20 changes: 20 additions & 0 deletions examples/defaulter-gen/output_tests/wholepkg/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// +k8s:defaulter-gen=TypeMeta

// This is a test package.
package wholepkg
46 changes: 46 additions & 0 deletions examples/defaulter-gen/output_tests/wholepkg/zz_generated.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// +build !ignore_autogenerated

/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// This file was autogenerated by defaulter-gen. Do not edit it manually!

package wholepkg

import (
runtime "k8s.io/apimachinery/pkg/runtime"
)

// RegisterDefaults adds defaulters functions to the given scheme.
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func RegisterDefaults(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&Struct_With_Field_Has_Manual_Defaulter{}, func(obj interface{}) {
SetObjectDefaults_Struct_With_Field_Has_Manual_Defaulter(obj.(*Struct_With_Field_Has_Manual_Defaulter))
})
scheme.AddTypeDefaultingFunc(&Struct_With_Field_With_Field_Has_Manual_Defaulter{}, func(obj interface{}) {
SetObjectDefaults_Struct_With_Field_With_Field_Has_Manual_Defaulter(obj.(*Struct_With_Field_With_Field_Has_Manual_Defaulter))
})
return nil
}

func SetObjectDefaults_Struct_With_Field_Has_Manual_Defaulter(in *Struct_With_Field_Has_Manual_Defaulter) {
SetDefaults_ManualDefault(&in.ManualDefault)
}

func SetObjectDefaults_Struct_With_Field_With_Field_Has_Manual_Defaulter(in *Struct_With_Field_With_Field_Has_Manual_Defaulter) {
SetObjectDefaults_Struct_With_Field_Has_Manual_Defaulter(&in.Struct_With_Field_Has_Manual_Defaulter)
}

0 comments on commit e0660ff

Please sign in to comment.