<commit_before>package function_test

import (
	"errors"
	"fmt"
	"testing"

	"github.com/xgfone/go-tools/function"
)

func get(i int, j int) (int, error) {
	return i + j, nil
}

func TestCall(t *testing.T) {
	if ret, err := function.Call(get, 1, 2); err != nil {
		t.Fail()
	} else {
		if ret[0].(int) != 3 || ret[1] != nil {
			t.Fail()
		}
	}
}

func ExampleCall() {
	f := func(i int, j int) (int, error) {
		return i + j, errors.New("This is not an error")
	}

	ret, _ := function.Call(f, 1, 2)

	// Since the first result is an integer, and it's not necessary to check
	// whether it is nil, so you may omit it, and infer this type directly.
	if ret[0] != nil {
		fmt.Println(ret[0].(int))
	}

	// Since the second result may be nil, so you MUST check whether it is nil firstly.
	if ret[1] != nil {
		fmt.Println(ret[1].(error))
	}
	// Output:
	// 3
	// This is not an error
}<commit_msg>Add the test of function/call<commit_after>package function_test

import (
	"errors"
	"fmt"
	"testing"

	"github.com/xgfone/go-tools/function"
)

func get(i int, j int) (int, error) {
	return i + j, nil
}

func TestCall(t *testing.T) {
	if ret, err := function.Call(get, 1, 2); err != nil {
		t.Fail()
	} else {
		if ret[0].(int) != 3 || ret[1] != nil {
			t.Fail()
		}
	}
}

func TestCallWithPointer(t *testing.T) {
	f := func(v *int) (old int) {
		old = *v
		*v += 1
		return
	}

	v := 1
	ret, _ := function.Call(f, &v)
	// The returned value is the old, which is 1, and v became 2.
	if ret[0].(int) != 1 || v != 2 {
		t.Fail()
	}
}

func ExampleCall() {
	f := func(i int, j int) (int, error) {
		return i + j, errors.New("This is not an error")
	}

	ret, _ := function.Call(f, 1, 2)

	// Since the first result is an integer, and it's not necessary to check
	// whether it is nil, so you may omit it, and infer this type directly.
	if ret[0] != nil {
		fmt.Println(ret[0].(int))
	}

	// Since the second result may be nil, so you MUST check whether it is nil firstly.
	if ret[1] != nil {
		fmt.Println(ret[1].(error))
	}
	// Output:
	// 3
	// This is not an error
}
{'fraction_non_alphanumeric': 0.1679611650485437, 'fraction_numerical': 0.015048543689320388, 'mean_word_length': 2.8379888268156424, 'pattern_counts': {'":': 0, '<': 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': '6161603', 'n_tokens_mistral': 877, 'n_tokens_neox': 818, 'n_words': 273}