1// Copyright 2020 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package regtest
6
7import (
8	"testing"
9)
10
11const sharedProgram = `
12-- go.mod --
13module mod
14
15go 1.12
16-- main.go --
17package main
18
19import "fmt"
20
21func main() {
22	fmt.Println("Hello World.")
23}`
24
25func runShared(t *testing.T, program string, testFunc func(env1 *Env, env2 *Env)) {
26	// Only run these tests in forwarded modes.
27	modes := runner.DefaultModes & (Forwarded | SeparateProcess)
28	runner.Run(t, sharedProgram, func(t *testing.T, env1 *Env) {
29		// Create a second test session connected to the same workspace and server
30		// as the first.
31		env2 := NewEnv(env1.Ctx, t, env1.Sandbox, env1.Server, env1.Editor.Config, true)
32		testFunc(env1, env2)
33	}, Modes(modes))
34}
35
36func TestSimultaneousEdits(t *testing.T) {
37	runShared(t, exampleProgram, func(env1 *Env, env2 *Env) {
38		// In editor #1, break fmt.Println as before.
39		env1.OpenFile("main.go")
40		env1.RegexpReplace("main.go", "Printl(n)", "")
41		// In editor #2 remove the closing brace.
42		env2.OpenFile("main.go")
43		env2.RegexpReplace("main.go", "\\)\n(})", "")
44
45		// Now check that we got different diagnostics in each environment.
46		env1.Await(env1.DiagnosticAtRegexp("main.go", "Printl"))
47		env2.Await(env2.DiagnosticAtRegexp("main.go", "$"))
48	})
49}
50
51func TestShutdown(t *testing.T) {
52	runShared(t, sharedProgram, func(env1 *Env, env2 *Env) {
53		env1.CloseEditor()
54		// Now make an edit in editor #2 to trigger diagnostics.
55		env2.OpenFile("main.go")
56		env2.RegexpReplace("main.go", "\\)\n(})", "")
57		env2.Await(env2.DiagnosticAtRegexp("main.go", "$"))
58	})
59}
60