<commit_before>package forms

import (
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
)

// TestFormIsValid tests data that come from request (url.Values)
func TestFormIsValid(t *testing.T) {
	postData := url.Values{}
	postData.Set("field1", "Foo")
	postData.Set("field2", "Bar")
	postData.Set("fieldX", "Ham")

	f := Form{
		Fields: map[string]*Field{
			"field1": &Field{
				Type: &Input{},
			},
			"field2": &Field{
				Type: &Input{},
			},
		},
	}

	assert.Equal(
		t, f.CleanedData, *new(CleanedData),
		"CleanedData should be empty at beggining")

	assert.True(t, f.IsValid(postData), "Form should pass")
	assert.Equal(
		t, f.CleanedData, CleanedData{"field1": "Foo", "field2": "Bar"},
		"Forms CleanedData field should contain cleaned data")

	assert.True(t, f.IsValid(url.Values{}), "Form should pass")
	assert.Equal(
		t, f.CleanedData, CleanedData{"field1": "", "field2": ""},
		"Form should pass")
}

// TestFromIsValidMap tests data that come from map
func TestFromIsValidMap(t *testing.T) {
	f := Form{
		Fields: map[string]*Field{
			"field1": &Field{
				Type: &Input{},
			},
			"field2": &Field{
				Type: &Input{},
			},
		},
	}

	values := map[string]interface{}{
		"field1": "Spam",
		"field2": []string{"Ham"},
	}

	assert.True(t, f.IsValidMap(values), "Form should pass")
	assert.Equal(
		t, f.CleanedData, CleanedData{"field1": "Spam", "field2": "Ham"},
		"Form should pass")

	values = map[string]interface{}{
		"field1": "Spam",
	}

	assert.True(t, f.IsValidMap(values), "Form should pass")
	assert.Equal(
		t, f.CleanedData, CleanedData{"field1": "Spam", "field2": ""},
		"Form should pass")
}

func TestDefaultFieldType(t *testing.T) {
	f := Form{
		Fields: map[string]*Field{
			"field1": &Field{},
			"field2": &Field{},
		},
	}

	values := map[string]interface{}{
		"field1": "Spam",
		"field2": []string{"Ham"},
	}

	assert.True(t, f.IsValidMap(values), "Form should pass")
}

func TestBasicValidation(t *testing.T) {
	f := Form{
		Fields: map[string]*Field{
			"field1": &Field{
				Validators: []Validator{
					&Required{},
				},
			},
			"field2": &Field{},
		},
	}

	values := map[string]interface{}{
		"field2": []string{"Ham"},
	}

	assert.False(t, f.IsValidMap(values), "Form shouldn't pass")
}

func TestFormNewFunc(t *testing.T) {
	f := New(
		map[string]*Field{
			"field1": &Field{},
			"field2": &Field{},
		},
		Attributes{"id": "test"},
	)

	assert.Equal(t, f.Fields["field1"].Name, "field1", "Field name should propagate")
	assert.Equal(t, f.Fields["field2"].Name, "field2", "Field name should propagate")
	assert.Equal(t, f.Attributes, Attributes{"id": "test"}, "Attributes should be set")
}
<commit_msg>Additional test for nil attributes in New form<commit_after>package forms

import (
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
)

// TestFormIsValid tests data that come from request (url.Values)
func TestFormIsValid(t *testing.T) {
	postData := url.Values{
		"field1": []string{"Foo"},
		"field2": []string{"Bar"},
		"fieldX": []string{"Ham"},
	}

	f := Form{
		Fields: map[string]*Field{
			"field1": &Field{
				Type: &Input{},
			},
			"field2": &Field{
				Type: &Input{},
			},
		},
	}

	assert.Equal(
		t, f.CleanedData, *new(CleanedData),
		"CleanedData should be empty at beggining")

	assert.True(t, f.IsValid(postData), "Form should pass")
	assert.Equal(
		t, f.CleanedData, CleanedData{"field1": "Foo", "field2": "Bar"},
		"Forms CleanedData field should contain cleaned data")

	assert.True(t, f.IsValid(url.Values{}), "Form should pass")
	assert.Equal(
		t, f.CleanedData, CleanedData{"field1": "", "field2": ""},
		"Form should pass")
}

// TestFromIsValidMap tests data that come from map
func TestFromIsValidMap(t *testing.T) {
	f := Form{
		Fields: map[string]*Field{
			"field1": &Field{
				Type: &Input{},
			},
			"field2": &Field{
				Type: &Input{},
			},
		},
	}

	values := map[string]interface{}{
		"field1": "Spam",
		"field2": []string{"Ham"},
	}

	assert.True(t, f.IsValidMap(values), "Form should pass")
	assert.Equal(
		t, f.CleanedData, CleanedData{"field1": "Spam", "field2": "Ham"},
		"Form should pass")

	values = map[string]interface{}{
		"field1": "Spam",
	}

	assert.True(t, f.IsValidMap(values), "Form should pass")
	assert.Equal(
		t, f.CleanedData, CleanedData{"field1": "Spam", "field2": ""},
		"Form should pass")
}

func TestDefaultFieldType(t *testing.T) {
	f := Form{
		Fields: map[string]*Field{
			"field1": &Field{},
			"field2": &Field{},
		},
	}

	values := map[string]interface{}{
		"field1": "Spam",
		"field2": []string{"Ham"},
	}

	assert.True(t, f.IsValidMap(values), "Form should pass")
}

func TestBasicValidation(t *testing.T) {
	f := Form{
		Fields: map[string]*Field{
			"field1": &Field{
				Validators: []Validator{
					&Required{},
				},
			},
			"field2": &Field{},
		},
	}

	values := map[string]interface{}{
		"field2": []string{"Ham"},
	}

	assert.False(t, f.IsValidMap(values), "Form shouldn't pass")
}

func TestFormNewFunc(t *testing.T) {
	f := New(
		map[string]*Field{
			"field1": &Field{},
			"field2": &Field{},
		},
		nil,
	)

	assert.Equal(t, f.Attributes, Attributes(nil), "Attributes should be nil")

	f = New(
		map[string]*Field{
			"field1": &Field{},
			"field2": &Field{},
		},
		Attributes{"id": "test"},
	)

	assert.Equal(t, f.Fields["field1"].Name, "field1", "Field name should propagate")
	assert.Equal(t, f.Fields["field2"].Name, "field2", "Field name should propagate")
	assert.Equal(t, f.Attributes, Attributes{"id": "test"}, "Attributes should be set")
}

{'fraction_non_alphanumeric': 0.2005053239487457, 'fraction_numerical': 0.011189316007940805, 'mean_word_length': 4.263057929724597, 'pattern_counts': {'":': 57, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6289320', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2065, 'n_words': 460}