<commit_before>// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.

package commands

import (
	. "github.com/limetext/lime/backend"
	. "github.com/quarnster/util/text"
	"testing"
)

func TestIndent(t *testing.T) {
	type Test struct {
		text string
		expect string
		sel []Region
	}
	tests := []Test{
		{
			"Life is short\nYou need Python\n",
			"\tLife is short\n\tYou need Python\n",
			[]Region{{3, 5}, {9, 10}, {16, 20}},
		},
		{
			"Life is short\nYou need Python\n",
			"\tLife is short\n\tYou need Python\n",
			[]Region{{3, 20}},
		},
	}
	ed := GetEditor()
	w := ed.NewWindow()

	for i, test := range tests {
		v := w.NewFile()
		e := v.BeginEdit()
		v.Insert(e, 0, test.text)
		v.EndEdit(e)

		v.Sel().Clear()
		for _, r := range test.sel {
			v.Sel().Add(r)
		}

		ed.CommandHandler().RunTextCommand(v, "indent", nil)
		if d := v.Buffer().Substr(Region{0, v.Buffer().Size()}); d != test.expect {
			t.Errorf("Test %d: Excepted %s, but got %s", i, test.expect, d)<commit_msg>add more indent tests<commit_after>// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.

package commands

import (
	. "github.com/limetext/lime/backend"
	. "github.com/quarnster/util/text"
	"testing"
)

func TestIndent(t *testing.T) {
	type Test struct {
		text string
		translate_tabs_to_spaces interface{}
		tab_size interface{}
		sel []Region
		expect string
	}
	tests := []Test{
		{	// translate_tabs_to_spaces = false
			// indent should be "\t"
			"a\n b\n  c\n   d\n",
			false,
			4,
			[]Region{{0, 1}},
			"\ta\n b\n  c\n   d\n",
		},
		{	// translate_tabs_to_spaces = nil
			// indent should be "\t"
			"a\n b\n  c\n   d\n",
			nil,
			1,
			[]Region{{0, 1}},
			"\ta\n b\n  c\n   d\n",
		},
		{	// translate_tabs_to_spaces = true and tab_size = 2
			// indent should be "  "
			"a\n b\n  c\n   d\n",
			true,
			2,
			[]Region{{0, 1}},
			"  a\n b\n  c\n   d\n",
		},
		{	// translate_tabs_to_spaces = true and tab_size = nil
			// indent should be "    "
			"a\n b\n  c\n   d\n",
			true,
			nil,
			[]Region{{0, 1}},
			"    a\n b\n  c\n   d\n",
		},
		{	// region include the 1st line and the 4th line
			// indent should add to the begining of 1st and 4th line
			"a\n b\n  c\n   d\n",
			false,
			1,
			[]Region{{0, 1}, {11, 12}},
			"\ta\n b\n  c\n\t   d\n",
		},
	}
	ed := GetEditor()
	w := ed.NewWindow()

	for i, test := range tests {
		v := w.NewFile()
		e := v.BeginEdit()
		v.Insert(e, 0, test.text)
		v.EndEdit(e)

		v.Sel().Clear()
		for _, r := range test.sel {
			v.Sel().Add(r)
		}
		v.Settings().Set("translate_tabs_to_spaces", test.translate_tabs_to_spaces)
		v.Settings().Set("tab_size", test.tab_size)

		ed.CommandHandler().RunTextCommand(v, "indent", nil)
		if d := v.Buffer().Substr(Region{0, v.Buffer().Size()}); d != test.expect {
			t.Errorf("Test %d: Excepted %s, but got %s", i, test.expect, d)
{'fraction_non_alphanumeric': 0.1793766578249337, 'fraction_numerical': 0.016246684350132625, 'mean_word_length': 2.5577830188679247, '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': '4868395', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1218, 'n_words': 324}