<commit_before>package sling

import (
	"bytes"
	"errors"
	"fmt"
	"io/ioutil"
	"math"
	"net/http"
	"net/http/httptest"
	"net/url"
	"reflect"
	"testing"
)

type FakeParams struct {
	KindName string `url:"kind_name"`
	Count    int    `url:"count"`
}

// Url-tagged query struct
var paramsA = struct {
	Limit int `url:"limit"`
}{
	30,
}
var paramsB = FakeParams{KindName: "recent", Count: 25}

// Json-tagged model struct
type FakeModel struct {
	Text          string  `json:"text,omitempty"`
	FavoriteCount int64   `json:"favorite_count,omitempty"`
	Temperature   float64 `json:"temperature,omitempty"`
}

var modelA = FakeModel{Text: "note", FavoriteCount: 12}

func TestNew(t *testing.T) {
	sling := New()
	if sling.HttpClient != http.DefaultClient {
		t.Errorf("expected %v, got %v", http.DefaultClient, sling.HttpClient)
	}
	if sling.Header == nil {
		t.Errorf("Header map not initialized with make")
	}
	if sling.queryStructs == nil {
		t.Errorf("queryStructs not initialized with make")
	}
}

func TestSlingNew(t *testing.T) {
	cases := []*Sling{
		&Sling{HttpClient: &http.Client{}, Method: "GET", RawUrl: "http://example.com"},
		&Sling{HttpClient: nil, Method: "", RawUrl: "http://example.com"},
		&Sling{queryStructs: make([]interface{}, 0)},
		&Sling{queryStructs: []interface{}{paramsA}},
		&Sling{queryStructs: []interface{}{paramsA, paramsB}},
		&Sling{jsonBody: &FakeModel{Text: "a"}},
		&Sling{jsonBody: FakeModel{Text: "a"}},
		&Sling{jsonBody: nil},
		New().Add("Content-Type", "application/json"),
		New().Add("A", "B").Add("a", "c").New(),
		New().Add("A", "B").New().Add("a", "c"),
		New().BodyStruct(paramsB),
		New().BodyStruct(paramsB).New(),
	}
	for _, sling := range cases {
		child := sling.New()
		if child.HttpClient != sling.HttpClient {
			t.Errorf("expected %p, got %p", sling.HttpClient, child.HttpClient)
		}
		if child.Method != sling.Method {
			t.Errorf("expected %s, got %s", sling.Method, child.Method)
		}
		if child.RawUrl != sling.RawUrl {
			t.Errorf("expected %s, got %s", sling.RawUrl, child.RawUrl)
		}
		// Header should be a copy of parent Sling Header. For example, calling
		// baseSling.Add("k","v") should not mutate previously created child Slings
		if sling.Header != nil {
			// struct literal cases don't init Header in usual way, skip Header check
			if !reflect.DeepEqual(sling.Header, child.Header) {
				t.Errorf("not DeepEqual: expected %v, got %v", sling.Header, child.Header)
			}
			sling.Header.Add("K", "V")
			if child.Header.Get("K") != "" {
				t.Errorf("child.Header was a reference to original map, should be copy")
			}
		}
		// queryStruct slice should be a new slice with a copy of the contents
		if len(sling.queryStructs) > 0 {
			// mutating one slice should not mutate the other
			child.queryStructs[0] = nil
			if sling.queryStructs[0] == nil {
				t.Errorf("child.queryStructs was a re-slice, expected slice with copied contents")
			}
		}
		// jsonBody should be copied
		if child.jsonBody != sling.jsonBody {
			t.Errorf("expected %v, got %v")
		}
		// bodyStruct should be copied
		if child.bodyStruct != sling.bodyStruct {
			t.Errorf("expected %v, got %v", sling.bodyStruct, child.bodyStruct)
		}
	}
}

func TestClientSetter(t *testing.T) {
	developerClient := &http.Client{}
	cases := []struct {
		input    *http.Client
		expected *http.Client
	}{
		{nil, http.DefaultClient},
		{developerClient, developerClient},
	}
	for _, c := range cases {
		sling := New()
		sling.Client(c.input)
		if sling.HttpClient != c.expected {
			t.Errorf("expected %v, got %v", c.expected, sling.HttpClient)
		}
	}
}

func TestBaseSetter(t *testing.T) {<commit_msg>Add missing arguments to Errorf in TestSlingNew<commit_after>package sling

import (
	"bytes"
	"errors"
	"fmt"
	"io/ioutil"
	"math"
	"net/http"
	"net/http/httptest"
	"net/url"
	"reflect"
	"testing"
)

type FakeParams struct {
	KindName string `url:"kind_name"`
	Count    int    `url:"count"`
}

// Url-tagged query struct
var paramsA = struct {
	Limit int `url:"limit"`
}{
	30,
}
var paramsB = FakeParams{KindName: "recent", Count: 25}

// Json-tagged model struct
type FakeModel struct {
	Text          string  `json:"text,omitempty"`
	FavoriteCount int64   `json:"favorite_count,omitempty"`
	Temperature   float64 `json:"temperature,omitempty"`
}

var modelA = FakeModel{Text: "note", FavoriteCount: 12}

func TestNew(t *testing.T) {
	sling := New()
	if sling.HttpClient != http.DefaultClient {
		t.Errorf("expected %v, got %v", http.DefaultClient, sling.HttpClient)
	}
	if sling.Header == nil {
		t.Errorf("Header map not initialized with make")
	}
	if sling.queryStructs == nil {
		t.Errorf("queryStructs not initialized with make")
	}
}

func TestSlingNew(t *testing.T) {
	cases := []*Sling{
		&Sling{HttpClient: &http.Client{}, Method: "GET", RawUrl: "http://example.com"},
		&Sling{HttpClient: nil, Method: "", RawUrl: "http://example.com"},
		&Sling{queryStructs: make([]interface{}, 0)},
		&Sling{queryStructs: []interface{}{paramsA}},
		&Sling{queryStructs: []interface{}{paramsA, paramsB}},
		&Sling{jsonBody: &FakeModel{Text: "a"}},
		&Sling{jsonBody: FakeModel{Text: "a"}},
		&Sling{jsonBody: nil},
		New().Add("Content-Type", "application/json"),
		New().Add("A", "B").Add("a", "c").New(),
		New().Add("A", "B").New().Add("a", "c"),
		New().BodyStruct(paramsB),
		New().BodyStruct(paramsB).New(),
	}
	for _, sling := range cases {
		child := sling.New()
		if child.HttpClient != sling.HttpClient {
			t.Errorf("expected %p, got %p", sling.HttpClient, child.HttpClient)
		}
		if child.Method != sling.Method {
			t.Errorf("expected %s, got %s", sling.Method, child.Method)
		}
		if child.RawUrl != sling.RawUrl {
			t.Errorf("expected %s, got %s", sling.RawUrl, child.RawUrl)
		}
		// Header should be a copy of parent Sling Header. For example, calling
		// baseSling.Add("k","v") should not mutate previously created child Slings
		if sling.Header != nil {
			// struct literal cases don't init Header in usual way, skip Header check
			if !reflect.DeepEqual(sling.Header, child.Header) {
				t.Errorf("not DeepEqual: expected %v, got %v", sling.Header, child.Header)
			}
			sling.Header.Add("K", "V")
			if child.Header.Get("K") != "" {
				t.Errorf("child.Header was a reference to original map, should be copy")
			}
		}
		// queryStruct slice should be a new slice with a copy of the contents
		if len(sling.queryStructs) > 0 {
			// mutating one slice should not mutate the other
			child.queryStructs[0] = nil
			if sling.queryStructs[0] == nil {
				t.Errorf("child.queryStructs was a re-slice, expected slice with copied contents")
			}
		}
		// jsonBody should be copied
		if child.jsonBody != sling.jsonBody {
			t.Errorf("expected %v, got %v", sling.jsonBody, child.jsonBody)
		}
		// bodyStruct should be copied
		if child.bodyStruct != sling.bodyStruct {
			t.Errorf("expected %v, got %v", sling.bodyStruct, child.bodyStruct)
		}
	}
}

func TestClientSetter(t *testing.T) {
	developerClient := &http.Client{}
	cases := []struct {
		input    *http.Client
		expected *http.Client
	}{
		{nil, http.DefaultClient},
		{developerClient, developerClient},
	}
	for _, c := range cases {
		sling := New()
		sling.Client(c.input)
		if sling.HttpClient != c.expected {
			t.Errorf("expected %v, got %v", c.expected, sling.HttpClient)
		}
	}
}

func TestBaseSetter(t *testing.T) {
{'fraction_non_alphanumeric': 0.16375545851528384, 'fraction_numerical': 0.003820960698689956, 'mean_word_length': 3.9353535353535354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1491639', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2597, 'n_words': 650}