Esempio n. 1
0
func TestParent(t *testing.T) {
	fn := "testdata/Default.sublime-keymap"
	fnp := "testdata/test.sublime-keymap"
	var (
		bd KeyBindings
		p  HasKeyBindings
	)

	d, err := ioutil.ReadFile(fn)
	if err != nil {
		t.Fatalf("Couldn't read %s: %s", fn, err)
	}
	if err = loaders.LoadJSON(d, &bd); err != nil {
		t.Fatalf("Error loading json: %s", err)
	}

	d, err = ioutil.ReadFile(fnp)
	if err != nil {
		t.Fatalf("Couldn't read %s: %s", fn, err)
	}
	if err = loaders.LoadJSON(d, p.KeyBindings()); err != nil {
		t.Fatalf("Error loading json: %s", err)
	}

	bd.SetParent(&p)

	if cmd := bd.Parent().KeyBindings().Bindings[0].Command; cmd != "t2" {
		t.Errorf("Expected Command %s, but got %s", "t2", cmd)
	}
}
Esempio n. 2
0
func TestKeyBindingsAction(t *testing.T) {
	tests := []struct {
		kp     KeyPress
		retNil bool
		ck     string
	}{
		{
			KeyPress{Key: 'i'},
			false,
			"test3",
		},
		{
			KeyPress{Key: 'p'},
			false,
			"t2",
		},
		{
			KeyPress{Key: 'i', Ctrl: true},
			true,
			"",
		},
		{
			KeyPress{Key: 'c'},
			false,
			"t5",
		},
	}

	if d, err := ioutil.ReadFile("testdata/Default.sublime-keymap"); err != nil {
		t.Fatal(err)
	} else {
		var (
			bindings KeyBindings
			p        HasKeyBindings
		)
		loaders.LoadJSON(d, &bindings)

		if d, err = ioutil.ReadFile("testdata/test.sublime-keymap"); err != nil {
			t.Fatal(err)
		}
		loaders.LoadJSON(d, p.KeyBindings())
		bindings.SetParent(&p)

		for i, test := range tests {
			qc := func(key string, operator util.Op, operand interface{}, match_all bool) bool {
				return key == test.ck
			}
			b := bindings.Filter(test.kp)
			if a := b.Action(qc); test.retNil {
				if a != nil {
					t.Errorf("Test %d: Expected action to be nil but got %v", i, a)
				}
			} else if a.Context[0].Key != test.ck {
				t.Errorf("Test %d: Expected %s, but got %s", i, test.ck, a.Context[0].Key)
			}
		}
	}
}
Esempio n. 3
0
func TestKeyBindingsFilter(t *testing.T) {
	tests := []struct {
		kp    KeyPress
		count int
	}{
		{
			KeyPress{Key: 'i', Ctrl: true},
			2,
		},
		{
			KeyPress{Key: 'i'},
			1,
		},
	}

	if d, err := ioutil.ReadFile("testdata/Default.sublime-keymap"); err == nil {
		var bindings KeyBindings
		loaders.LoadJSON(d, &bindings)

		for i, test := range tests {
			if b := bindings.Filter(test.kp); b.Len() != test.count {
				t.Errorf("Test %d: Expected %d bindings, but got %d", i, test.count, b.Len())
			}
		}
	}
}
Esempio n. 4
0
func TestVintageous(t *testing.T) {
	fn := "testdata/Vintageous.sublime-keymap"
	ed := GetEditor()
	w := ed.NewWindow()
	v := w.NewFile()
	v.Settings().Set("command_mode", true)

	OnQueryContext.Add(func(v *View, key string, op Op, operand interface{}, match_all bool) QueryContextReturn {
		if key == "vi_has_action" {
			return True
		}
		return Unknown
	})

	if d, err := ioutil.ReadFile(fn); err != nil {
		t.Errorf("Couldn't load file %s: %s", fn, err)
	} else {
		var bindings KeyBindings
		if err := loaders.LoadJSON(d, &bindings); err != nil {
			t.Error(err)
		}

		b2 := bindings.Filter(KeyPress{Key: 'g'})
		if a := b2.Action(v); a == nil || a.Command != "set_action" {
			t.Error(a)
		}
		b2 = b2.Filter(KeyPress{Key: 'g'})
		if a := b2.Action(v); a == nil || a.Command != "set_motion" {
			t.Error(a)
		}
	}
}
Esempio n. 5
0
func TestKeyFilter2(t *testing.T) {
	ed := GetEditor()
	w := ed.NewWindow()
	v := w.NewFile()
	enable := "test1"
	OnQueryContext.Add(func(v *View, key string, operator Op, operand interface{}, match_all bool) QueryContextReturn {
		if key == enable {
			return True
		}
		return Unknown
	})
	fn := "testdata/Default.sublime-keymap"
	if d, err := ioutil.ReadFile(fn); err != nil {
		t.Errorf("Couldn't load file %s: %s", fn, err)
	} else {
		var bindings KeyBindings
		if err := loaders.LoadJSON(d, &bindings); err != nil {
			t.Error(err)
		}
		b2 := bindings.Filter(KeyPress{Key: 'i'})
		a := b2.Action(v)
		if a.Context[0].Key != enable {
			t.Error(b2, a)
		}
	}
}
Esempio n. 6
0
func TestKeyBindingsAction(t *testing.T) {
	tests := []struct {
		kp KeyPress
		ck string
	}{
		{
			KeyPress{Key: 'i'},
			"test3",
		},
	}

	if d, err := ioutil.ReadFile("testdata/Default.sublime-keymap"); err == nil {
		var bindings KeyBindings
		loaders.LoadJSON(d, &bindings)

		for i, test := range tests {
			qc := func(key string, operator util.Op, operand interface{}, match_all bool) bool {
				return key == test.ck
			}
			b := bindings.Filter(test.kp)
			if a := b.Action(qc); a.Context[0].Key != test.ck {
				t.Errorf("Test %d: Expected %s, but got %s", i, test.ck, a.Context[0].Key)
			}
		}
	}
}
Esempio n. 7
0
func (e *Editor) loadSetting(pkg *packet) {
	if err := loaders.LoadJSON(pkg.Get().([]byte), e.Settings()); err != nil {
		log4go.Error(err)
	} else {
		log4go.Info("Loaded %s", pkg.Name())
		e.Watch(NewWatchedPackage(pkg))
	}
}
Esempio n. 8
0
func (e *Editor) loadKeyBinding(pkg *packet) {
	if err := loaders.LoadJSON(pkg.Get().([]byte), pkg); err != nil {
		log4go.Error(err)
	} else {
		log4go.Info("Loaded %s", pkg.Name())
		e.Watch(NewWatchedPackage(pkg))
	}
	e.keyBindings.merge(pkg.marshalTo.(*KeyBindings))
}
Esempio n. 9
0
func TestSetParent(t *testing.T) {
	fn := "testdata/Default.sublime-keymap"
	fnp := "testdata/test.sublime-keymap"
	var (
		bd KeyBindings
		p  HasKeyBindings
	)

	d, err := ioutil.ReadFile(fn)
	if err != nil {
		t.Fatalf("Couldn't read %s: %s", fn, err)
	}
	if err = loaders.LoadJSON(d, &bd); err != nil {
		t.Fatalf("Error loading json: %s", err)
	}
	d, err = ioutil.ReadFile(fnp)
	if err != nil {
		t.Fatalf("Couldn't read %s: %s", fn, err)
	}
	if err = loaders.LoadJSON(d, p.KeyBindings()); err != nil {
		t.Fatalf("Error loading json: %s", err)
	}

	p.KeyBindings().seqIndex = 10
	bd.SetParent(&p)
	if bd.seqIndex != p.KeyBindings().seqIndex {
		t.Fatalf("Expected parent and child seqIndex be equal %d != %d", p.KeyBindings().seqIndex, bd.seqIndex)
	}

	ret := bd.Filter(KeyPress{Key: 'd', Ctrl: true})
	if ret.Len() != 1 {
		t.Fatalf("Expected ret keyBindings len %d, but got %d", 1, ret.Len())
	}
	if ret.parent.KeyBindings().Len() != 1 {
		t.Fatalf("Expected ret parent keyBindings len %d, but got %d", 1, ret.parent.KeyBindings().Len())
	}
	if cmd := ret.Bindings[0].Command; cmd != "test4" {
		t.Errorf("Expected Command %s, but got %s", "test4", cmd)
	}
	if cmd := ret.parent.KeyBindings().Bindings[0].Command; cmd != "t1" {
		t.Errorf("Expected Command %s, but got %s", "t1", cmd)
	}
}
Esempio n. 10
0
func (e *Editor) loadSetting(path string) {
	d, err := ioutil.ReadFile(path)
	if err != nil {
		log4go.Error("Couldn't load file %s: %s", path, err)
	}
	if err := loaders.LoadJSON(d, e.Settings()); err != nil {
		log4go.Error(err)
	} else {
		log4go.Info("Loaded %s", path)
	}
}
Esempio n. 11
0
func (e *Editor) loadKeybinding(fn string) {
	d, err := ioutil.ReadFile(fn)
	if err != nil {
		log4go.Error("Couldn't load file %s: %s", fn, err)
	}
	var bindings KeyBindings
	if err := loaders.LoadJSON(d, &bindings); err != nil {
		log4go.Error(err)
	} else {
		log4go.Info("Loaded %s", fn)
	}
	e.keyBindings.merge(&bindings)
}
Esempio n. 12
0
func TestLoadKeyBindingsFromJSON(t *testing.T) {
	tests := []string{
		"testdata/Default.sublime-keymap",
	}
	for i, fn := range tests {
		if d, err := ioutil.ReadFile(fn); err != nil {
			t.Errorf("Test %d: Couldn't load file %s: %s", i, fn, err)
		} else {
			var bindings KeyBindings
			if err := loaders.LoadJSON(d, &bindings); err != nil {
				t.Errorf("Test %d: Error on LoadJSON: %s", i, err)
			}
		}
	}
}
Esempio n. 13
0
func TestDropLessEqualKeys(t *testing.T) {
	fn := "testdata/Default.sublime-keymap"
	d, err := ioutil.ReadFile(fn)
	if err != nil {
		t.Fatalf("Couldn't read %s: %s", fn, err)
	}

	var bd KeyBindings
	if err = loaders.LoadJSON(d, &bd); err != nil {
		t.Fatalf("Error loading json: %s", err)
	}
	bd.DropLessEqualKeys(1)
	if cmd := bd.Bindings[0].Command; cmd != "test2" {
		t.Errorf("Expected Command %s, but got %s", "test2", cmd)
	}
}
Esempio n. 14
0
func TestKeyFilter(t *testing.T) {
	fn := "loaders/json/testdata/Default (OSX).sublime-keymap"
	if d, err := ioutil.ReadFile(fn); err != nil {
		t.Errorf("Couldn't load file %s: %s", fn, err)
	} else {
		var bindings KeyBindings
		if err := loaders.LoadJSON(d, &bindings); err != nil {
			t.Error(err)
		}

		if b2 := bindings.Filter(KeyPress{Key: 'j', Ctrl: true}); b2.Len() != 3 {
			t.Errorf("Not of the expected length: %d, %s", 3, b2)
		} else if b3 := b2.Filter(KeyPress{Key: 's'}); b3.Len() != 1 {
			t.Errorf("Not of the expected length: %d, %s", 1, b3)
		}
	}
}
Esempio n. 15
0
func TestLoadKeyBindingsFromJSON(t *testing.T) {
	tests := []string{
		"loaders/json/testdata/Default (OSX).sublime-keymap",
	}
	for i, fn := range tests {
		if d, err := ioutil.ReadFile(fn); err != nil {
			if i == 0 {
				t.Errorf("Couldn't load file %s: %s", fn, err)
			}
		} else {
			var bindings KeyBindings
			if err := loaders.LoadJSON(d, &bindings); err != nil {
				t.Error(err)
			}
		}
	}
}
Esempio n. 16
0
func TestKeyBindingsString(t *testing.T) {
	fn := "testdata/test.sublime-keymap"
	var bd KeyBindings

	d, err := ioutil.ReadFile(fn)
	if err != nil {
		t.Fatalf("Couldn't read %s: %s", fn, err)
	}
	if err = loaders.LoadJSON(d, &bd); err != nil {
		t.Fatalf("Error loading json: %s", err)
	}

	expected :=
		`&{Keys:[p] Command:t2 Args:map[] Context:[{rawKeyContext:{Key:t2 Operator:0 Operand:true MatchAll:false}}] priority:1}
&{Keys:[ctrl+d ctrl+k] Command:t1 Args:map[] Context:[{rawKeyContext:{Key:t1 Operator:0 Operand:true MatchAll:false}}] priority:0}
`
	if bd.String() != expected {
		t.Errorf("Expected String %s, but got %s", expected, bd.String())
	}
}
Esempio n. 17
0
func (p *Packet) Load() error {
	return loaders.LoadJSON(p.Get().([]byte), p)
}