<commit_before>package javascript_def

import (
	"encoding/json"
	"testing"

	"github.com/jmoiron/sqlx/types"
	"sourcegraph.com/sourcegraph/srclib/graph"
)

func defDataJSON(si defData) types.JsonText {
	b, err := json.Marshal(si)
	if err != nil {
		panic(err)
	}
	return b
}

func TestDefFormatter_Name(t *testing.T) {
	tests := []struct {
		def  *graph.Def
		qual graph.Qualification
		want string
	}{
		{
			// unqualified
			def: &graph.Def{
				Name: "name",
				Data: types.JsonText(`{}`),
			},
			qual: graph.Unqualified,
			want: "name",
		},
		{
			// qualify defs with scope
			def: &graph.Def{
				Data: defDataJSON(defData{Key: DefPath{Path: "a.b"}}),
			},
			qual: graph.ScopeQualified,
			want: "a.b",
		},
		{
			// qualify file defs with scope
			def: &graph.Def{
				Data: defDataJSON(defData{Key: DefPath{Namespace: "file", Path: "a.b.@local123.c.d"}}),
			},
			qual: graph.ScopeQualified,
			want: "c.d",
		},
		{
			// qualify defs with module basename (dep-qualified)
			def: &graph.Def{<commit_msg>update srclib<commit_after>package javascript_def

import (
	"encoding/json"
	"testing"

	"sourcegraph.com/sourcegraph/srclib/graph"
	"sourcegraph.com/sourcegraph/srclib/util/sqltypes"
)

func defDataJSON(si defData) sqltypes.JSON {
	b, err := json.Marshal(si)
	if err != nil {
		panic(err)
	}
	return b
}

func TestDefFormatter_Name(t *testing.T) {
	tests := []struct {
		def  *graph.Def
		qual graph.Qualification
		want string
	}{
		{
			// unqualified
			def: &graph.Def{
				Name: "name",
				Data: sqltypes.JSON(`{}`),
			},
			qual: graph.Unqualified,
			want: "name",
		},
		{
			// qualify defs with scope
			def: &graph.Def{
				Data: defDataJSON(defData{Key: DefPath{Path: "a.b"}}),
			},
			qual: graph.ScopeQualified,
			want: "a.b",
		},
		{
			// qualify file defs with scope
			def: &graph.Def{
				Data: defDataJSON(defData{Key: DefPath{Namespace: "file", Path: "a.b.@local123.c.d"}}),
			},
			qual: graph.ScopeQualified,
			want: "c.d",
		},
		{
			// qualify defs with module basename (dep-qualified)
			def: &graph.Def{
{'fraction_non_alphanumeric': 0.16238487639360155, 'fraction_numerical': 0.00290838584585555, 'mean_word_length': 3.1362725450901805, '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': '499987', 'n_tokens_mistral': 933, 'n_tokens_neox': 801, 'n_words': 162}