<commit_before>// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gvalid_test

import (
	"testing"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/test/gtest"
	"github.com/gogf/gf/util/gvalid"
)

func Test_Check(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "abc:6,16"
		val1 := 0
		val2 := 7
		val3 := 20
		err1 := gvalid.Check(val1, rule, nil)
		err2 := gvalid.Check(val2, rule, nil)
		err3 := gvalid.Check(val3, rule, nil)
		t.Assert(err1, "invalid rules: abc:6,16")
		t.Assert(err2, "invalid rules: abc:6,16")
		t.Assert(err3, "invalid rules: abc:6,16")
	})
}

func Test_Required(t *testing.T) {
	if m := gvalid.Check("1", "required", nil); m != nil {
		t.Error(m)
	}
	if m := gvalid.Check("", "required", nil); m == nil {
		t.Error(m)
	}
	if m := gvalid.Check("", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 1, "age": 19}); m == nil {
		t.Error("Required校验失败")
	}
	if m := gvalid.Check("", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 2, "age": 19}); m != nil {
		t.Error("Required校验失败")
	}
}

func Test_RequiredIf(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-if:id,1,age,18"
		t.AssertNE(gvalid.Check("", rule, nil, g.Map{"id": 1}), nil)
		t.Assert(gvalid.Check("", rule, nil, g.Map{"id": 0}), nil)
		t.AssertNE(gvalid.Check("", rule, nil, g.Map{"age": 18}), nil)
		t.Assert(gvalid.Check("", rule, nil, g.Map{"age": 20}), nil)
	})
}

func Test_RequiredUnless(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-unless:id,1,age,18"
		t.Assert(gvalid.Check("", rule, nil, g.Map{"id": 1}), nil)
		t.AssertNE(gvalid.Check("", rule, nil, g.Map{"id": 0}), nil)
		t.Assert(gvalid.Check("", rule, nil, g.Map{"age": 18}), nil)
		t.AssertNE(gvalid.Check("", rule, nil, g.Map{"age": 20}), nil)
	})
}

func Test_RequiredWith(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-with:id,name"
		val1 := ""
		params1 := g.Map{
			"age": 18,
		}
		params2 := g.Map{
			"id": 100,
		}
		params3 := g.Map{
			"id":   100,
			"name": "john",
		}
		err1 := gvalid.Check(val1, rule, nil, params1)
		err2 := gvalid.Check(val1, rule, nil, params2)
		err3 := gvalid.Check(val1, rule, nil, params3)
		t.Assert(err1, nil)
		t.AssertNE(err2, nil)
		t.AssertNE(err3, nil)
	})
}

func Test_RequiredWithAll(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-with-all:id,name"
		val1 := ""
		params1 := g.Map{
			"age": 18,
		}
		params2 := g.Map{
			"id": 100,
		}
		params3 := g.Map{
			"id":   100,
			"name": "john",
		}
		err1 := gvalid.Check(val1, rule, nil, params1)
		err2 := gvalid.Check(val1, rule, nil, params2)
		err3 := gvalid.Check(val1, rule, nil, params3)
		t.Assert(err1, nil)
		t.Assert(err2, nil)
		t.AssertNE(err3, nil)
	})
}

func Test_RequiredWithOut(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-without:id,name"
		val1 := ""
		params1 := g.Map{
			"age": 18,
		}
		params2 := g.Map{
			"id": 100,
		}
		params3 := g.Map{
			"id":   100,
			"name": "john",
		}
		err1 := gvalid.Check(val1, rule, nil, params1)
		err2 := gvalid.Check(val1, rule, nil, params2)
		err3 := gvalid.Check(val1, rule, nil, params3)
		t.AssertNE(err1, nil)
		t.AssertNE(err2, nil)
		t.Assert(err3, nil)
	})
}

func Test_RequiredWithOutAll(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-without-all:id,name"
		val1 := ""
		params1 := g.Map{
			"age": 18,
		}
		params2 := g.Map{
			"id": 100,
		}
		params3 := g.Map{
			"id":   100,
			"name": "john",
		}
		err1 := gvalid.Check(val1, rule, nil, params1)
		err2 := gvalid.Check(val1, rule, nil, params2)
		err3 := gvalid.Check(val1, rule, nil, params3)
		t.AssertNE(err1, nil)
		t.Assert(err2, nil)
		t.Assert(err3, nil)
	})
}

func Test_Date(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "date"
		val1 := "2010"
		val2 := "201011"
		val3 := "20101101"
		val4 := "2010-11-01"
		val5 := "2010.11.01"
		val6 := "2010/11/01"
		err1 := gvalid.Check(val1, rule, nil)
		err2 := gvalid.Check(val2, rule, nil)
		err3 := gvalid.Check(val3, rule, nil)
		err4 := gvalid.Check(val4, rule, nil)
		err5 := gvalid.Check(val5, rule, nil)
		err6 := gvalid.Check(val6, rule, nil)
		t.AssertNE(err1, nil)
		t.AssertNE(err2, nil)
		t.Assert(err3, nil)
		t.Assert(err4, nil)
		t.Assert(err5, nil)
		t.Assert(err6, nil)
	})
}

func Test_DateFormat(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		val1 := "2010"
		val2 := "201011"
		val3 := "2010.11"
		val4 := "201011-01"
		val5 := "2010~11~01"
		val6 := "2010-11~01"<commit_msg>fix issue in unit testing case of package gvalid<commit_after>// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gvalid_test

import (
	"testing"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/test/gtest"
	"github.com/gogf/gf/util/gvalid"
)

func Test_Check(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "abc:6,16"
		val1 := 0
		val2 := 7
		val3 := 20
		err1 := gvalid.Check(val1, rule, nil)
		err2 := gvalid.Check(val2, rule, nil)
		err3 := gvalid.Check(val3, rule, nil)
		t.Assert(err1, "invalid rules: abc:6,16")
		t.Assert(err2, "invalid rules: abc:6,16")
		t.Assert(err3, "invalid rules: abc:6,16")
	})
}

func Test_Required(t *testing.T) {
	if m := gvalid.Check("1", "required", nil); m != nil {
		t.Error(m)
	}
	if m := gvalid.Check("", "required", nil); m == nil {
		t.Error(m)
	}
	if m := gvalid.Check("", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 1, "age": 19}); m == nil {
		t.Error("Required校验失败")
	}
	if m := gvalid.Check("", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 2, "age": 19}); m != nil {
		t.Error("Required校验失败")
	}
}

func Test_RequiredIf(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-if:id,1,age,18"
		t.AssertNE(gvalid.Check("", rule, nil, g.Map{"id": 1}), nil)
		t.Assert(gvalid.Check("", rule, nil, g.Map{"id": 0}), nil)
		t.AssertNE(gvalid.Check("", rule, nil, g.Map{"age": 18}), nil)
		t.Assert(gvalid.Check("", rule, nil, g.Map{"age": 20}), nil)
	})
}

func Test_RequiredUnless(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-unless:id,1,age,18"
		t.Assert(gvalid.Check("", rule, nil, g.Map{"id": 1}), nil)
		t.AssertNE(gvalid.Check("", rule, nil, g.Map{"id": 0}), nil)
		t.Assert(gvalid.Check("", rule, nil, g.Map{"age": 18}), nil)
		t.AssertNE(gvalid.Check("", rule, nil, g.Map{"age": 20}), nil)
	})
}

func Test_RequiredWith(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-with:id,name"
		val1 := ""
		params1 := g.Map{
			"age": 18,
		}
		params2 := g.Map{
			"id": 100,
		}
		params3 := g.Map{
			"id":   100,
			"name": "john",
		}
		err1 := gvalid.Check(val1, rule, nil, params1)
		err2 := gvalid.Check(val1, rule, nil, params2)
		err3 := gvalid.Check(val1, rule, nil, params3)
		t.Assert(err1, nil)
		t.AssertNE(err2, nil)
		t.AssertNE(err3, nil)
	})
}

func Test_RequiredWithAll(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-with-all:id,name"
		val1 := ""
		params1 := g.Map{
			"age": 18,
		}
		params2 := g.Map{
			"id": 100,
		}
		params3 := g.Map{
			"id":   100,
			"name": "john",
		}
		err1 := gvalid.Check(val1, rule, nil, params1)
		err2 := gvalid.Check(val1, rule, nil, params2)
		err3 := gvalid.Check(val1, rule, nil, params3)
		t.Assert(err1, nil)
		t.Assert(err2, nil)
		t.AssertNE(err3, nil)
	})
}

func Test_RequiredWithOut(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-without:id,name"
		val1 := ""
		params1 := g.Map{
			"age": 18,
		}
		params2 := g.Map{
			"id": 100,
		}
		params3 := g.Map{
			"id":   100,
			"name": "john",
		}
		err1 := gvalid.Check(val1, rule, nil, params1)
		err2 := gvalid.Check(val1, rule, nil, params2)
		err3 := gvalid.Check(val1, rule, nil, params3)
		t.AssertNE(err1, nil)
		t.AssertNE(err2, nil)
		t.Assert(err3, nil)
	})
}

func Test_RequiredWithOutAll(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "required-without-all:id,name"
		val1 := ""
		params1 := g.Map{
			"age": 18,
		}
		params2 := g.Map{
			"id": 100,
		}
		params3 := g.Map{
			"id":   100,
			"name": "john",
		}
		err1 := gvalid.Check(val1, rule, nil, params1)
		err2 := gvalid.Check(val1, rule, nil, params2)
		err3 := gvalid.Check(val1, rule, nil, params3)
		t.AssertNE(err1, nil)
		t.Assert(err2, nil)
		t.Assert(err3, nil)
	})
}

func Test_Date(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		rule := "date"
		val1 := "2010"
		val2 := "201011"
		val3 := "20101101"
		val4 := "2010-11-01"
		val5 := "2010.11.01"
		val6 := "2010/11/01"
		val7 := "2010=11=01"
		err1 := gvalid.Check(val1, rule, nil)
		err2 := gvalid.Check(val2, rule, nil)
		err3 := gvalid.Check(val3, rule, nil)
		err4 := gvalid.Check(val4, rule, nil)
		err5 := gvalid.Check(val5, rule, nil)
		err6 := gvalid.Check(val6, rule, nil)
		err7 := gvalid.Check(val7, rule, nil)
		t.Assert(err1, nil)
		t.Assert(err2, nil)
		t.Assert(err3, nil)
		t.Assert(err4, nil)
		t.Assert(err5, nil)
		t.Assert(err6, nil)
		t.AssertNE(err7, nil)
	})
}

func Test_DateFormat(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		val1 := "2010"
		val2 := "201011"
		val3 := "2010.11"
		val4 := "201011-01"
		val5 := "2010~11~01"
		val6 := "2010-11~01"
{'fraction_non_alphanumeric': 0.21014644351464434, 'fraction_numerical': 0.05794979079497908, 'mean_word_length': 3.4698457223001404, 'pattern_counts': {'":': 56, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6707884', 'n_tokens_mistral': 4805, 'n_tokens_neox': 4290, 'n_words': 901}