package utils

import (
	"testing"
)

func TestSanitize(t *testing.T) {
	if res := Sanitize("abcdef0123ABCDEFéçè_ ?.!=îôù"); res != "abcdef0123abcdefece------iou" {
		t.Errorf("unexpected Sanitize() answer '%s' != 'abcdef0123abcdefece------iou'", res)
	}
}

func TestArrayContains(t *testing.T) {
	if ArrayPointerContains(nil, "toto") {
		t.Errorf("unexpected contains true for nil array")
	}
	arr := []string{"bird", "apple", "ocean", "fork", "anchor"}
	if !ArrayPointerContains(&arr, "bird") {
		t.Errorf("unexpected contains false")
	}
	arr = []string{"bird", "apple", "ocean", "fork", "anchor"}
	if ArrayPointerContains(&arr, "potato") {
		t.Errorf("unexpected contains true")
	}
}

func TestAtoI(t *testing.T) {
	if res := AtoI("3"); res != 3 {
		t.Errorf("unexpected answer %d != 3", res)
	}
}

func TestEnglishDateString(t *testing.T) {
	if res := EnglishDateString("Mercredi 03 février 2021"); res != "Wednesday 03 February 2021" {
		t.Errorf("unexpected date format : %s", res)
	}
}

func TestStringPointer(t *testing.T) {
	if res := StringPointer(""); res != nil {
		t.Errorf("unexpected res : %s", *res)
	}
	if res := StringPointer("toto"); res == nil {
		t.Errorf("unexpected res : nil")
	} else {
		if *res != "toto" {
			t.Errorf("unexpected res : %s", *res)
		}
	}
}

func TestIntPointer(t *testing.T) {
	if res := IntPointer(0); res != nil {
		t.Errorf("unexpected res : %d", *res)
	}
	if res := IntPointer(123); res == nil {
		t.Errorf("unexpected res : nil")
	} else if *res != 123 {
		t.Errorf("unexpected res : %d", *res)
	}
}

func TestArrayPointerAppend(t *testing.T) {
	var arr *[]string
	if arr = ArrayPointerAppend(arr, "toto"); arr == nil {
		t.Errorf("unexpected arr : nil")
	} else if len(*arr) != 1 {
		t.Errorf("unexpected arr len : %d", len(*arr))
	} else if (*arr)[0] != "toto" {
		t.Errorf("unexpected arr content : %s", *arr)
	}

	if arr = ArrayPointerAppend(arr, "test"); arr == nil {
		t.Errorf("unexpected arr : nil")
	} else if len(*arr) != 2 {
		t.Errorf("unexpected arr len : %d", len(*arr))
	} else if (*arr)[1] != "test" {
		t.Errorf("unexpected arr content : %s", *arr)
	}
}

func TestArrayPointerJoin(t *testing.T) {
	if s := ArrayPointerJoin(nil, "-"); s != "" {
		t.Errorf("unexpected join result : %s", s)
	}
	arr := []string{"toto", "test"}
	if s:= ArrayPointerJoin(&arr, "-"); s != "toto-test" {
		t.Errorf("unexpected join result : %s", s)
	}
}
