testutil

package module
v0.0.0-...-40a09a0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2025 License: MIT Imports: 7 Imported by: 1

README

テストで利用するutil関数群

  • AssertJsonExactをはじめとしたgo testの際に利用する関数群

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertComp

func AssertComp(t *testing.T, target string, expect string)

func AssertContainStr

func AssertContainStr(t *testing.T, target any, str string)

func AssertDeepEqual

func AssertDeepEqual(t *testing.T, target any, expect any)

func AssertEqual

func AssertEqual(t *testing.T, target any, expect any)

func AssertEqualFatal

func AssertEqualFatal(t *testing.T, target any, expect any)

func AssertErrorAs

func AssertErrorAs(t *testing.T, target error, expect any)

func AssertFalse

func AssertFalse(t *testing.T, target any)

func AssertJsonExact

func AssertJsonExact(t *testing.T, targetJson string, expectJson string, ignoreValues []string)

文字列のjsonを受け取ってmapにしてDiffを取る。 完全一致比較。(余計なフィールドがあってもエラーとなり、不足してもエラーとなる)

現状、最上位が{}で囲まれているJSONのみに対応している これは、json.Unmarshalした際に、最上位がmap[string]interface{}となるもので、 例えば、[0,1,2,3] や 3 はエラーとなる。

ignoreValuesで指定したフィールドはデフォルト値とすることができる。 ignoreValuesはドット区切りで指定する。(例)data.entity.user.id したがってフィールドが存在することをチェックしたいが値自体には関心がない場合に、 ignoreValuesを指定した上で、expectJsonの対象のフィールドを stringなら"", intなら0, boolならfalse, に指定すれば良い。

(24/8/17対応)ignoreValuesは途中が配列の場合は全ての子要素に適用される。 例えば"data.id"のように指定した際に、{"data": [{"id":1}, {"id":2}, ...]}の全ての子要素に 適用される。ただし{"data": [[ {"id":1} ]] }のように入れ子の配列には適用されない。

ブロックを指定(例えば、data.entity.userとした際にuser内に各種キーが含まれる場合)した場合は、その中身が再帰的に全てデフォルト値になる。 (user内のプロパティが消えて空のマップとなるわけではなく、中のキーそれぞれがデフォルト値になる点に注意。) *.fieldNameのように指定すると階層に関係なくすべての"fieldName" をデフォルト値にする。(ただ、mapを全走査するので効率は悪い。) *は必ず先頭に置いて直後にフィールドを指定する必要がある。 aaa.*.fieldNameや、*.aaa.fieldNameといった指定はできない。

日時はJSON上は単なる文字列のため、ゼロバリューは空文字となる。

func AssertNotDeepEqual

func AssertNotDeepEqual(t *testing.T, target any, expect any)

func AssertNotEqual

func AssertNotEqual(t *testing.T, target any, shouldNotBe any)

func AssertNotEqualFatal

func AssertNotEqualFatal(t *testing.T, target any, shouldNotBe any)

func AssertNotTypedNil

func AssertNotTypedNil[T any](t *testing.T, target *T)

func AssertNotUnTypedNil

func AssertNotUnTypedNil(t *testing.T, target any)

func AssertTrue

func AssertTrue(t *testing.T, target any)

func AssertTypedNil

func AssertTypedNil[T any](t *testing.T, target *T)

func AssertUnTypedNil

func AssertUnTypedNil(t *testing.T, target any)

func GetFirst

func GetFirst[A any](a A, b ...any) A

func GetLast

func GetLast(a ...any) any

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL