2017-03-01 16:16:22 -06:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-13 15:01:19 -05:00
|
|
|
"regexp"
|
2017-03-01 16:16:22 -06:00
|
|
|
"testing"
|
2017-03-13 15:09:25 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/mitchellh/colorstring"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/plans"
|
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2017-03-01 16:16:22 -06:00
|
|
|
)
|
|
|
|
|
2017-03-13 15:09:25 -05:00
|
|
|
func TestUiHookPreApply_periodicTimer(t *testing.T) {
|
2017-08-14 10:19:40 -05:00
|
|
|
ui := cli.NewMockUi()
|
2017-03-13 15:09:25 -05:00
|
|
|
h := &UiHook{
|
|
|
|
Colorize: &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
|
|
|
Disable: true,
|
|
|
|
Reset: true,
|
|
|
|
},
|
|
|
|
Ui: ui,
|
|
|
|
PeriodicUiTimer: 1 * time.Second,
|
|
|
|
}
|
|
|
|
h.init()
|
|
|
|
h.resources = map[string]uiResourceState{
|
|
|
|
"data.aws_availability_zones.available": uiResourceState{
|
|
|
|
Op: uiResourceDestroy,
|
|
|
|
Start: time.Now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
addr := addrs.Resource{
|
|
|
|
Mode: addrs.DataResourceMode,
|
|
|
|
Type: "aws_availability_zones",
|
|
|
|
Name: "available",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
2018-10-13 12:47:46 -05:00
|
|
|
priorState := cty.ObjectVal(map[string]cty.Value{
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
"id": cty.StringVal("2017-03-05 10:56:59.298784526 +0000 UTC"),
|
|
|
|
"names": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("us-east-1a"),
|
|
|
|
cty.StringVal("us-east-1b"),
|
|
|
|
cty.StringVal("us-east-1c"),
|
|
|
|
cty.StringVal("us-east-1d"),
|
|
|
|
}),
|
|
|
|
})
|
2018-10-13 12:47:46 -05:00
|
|
|
plannedNewState := cty.NullVal(cty.Object(map[string]cty.Type{
|
|
|
|
"id": cty.String,
|
|
|
|
"names": cty.List(cty.String),
|
|
|
|
}))
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
|
|
|
|
action, err := h.PreApply(addr, states.CurrentGen, plans.Delete, priorState, plannedNewState)
|
2017-03-13 15:09:25 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(3100 * time.Millisecond)
|
|
|
|
|
2017-03-17 10:04:27 -05:00
|
|
|
// stop the background writer
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
uiState := h.resources[addr.String()]
|
2017-03-17 10:04:27 -05:00
|
|
|
close(uiState.DoneCh)
|
|
|
|
<-uiState.done
|
|
|
|
|
2018-10-13 12:47:46 -05:00
|
|
|
expectedOutput := `data.aws_availability_zones.available: Destroying... [id=2017-03-05 10:56:59.298784526 +0000 UTC]
|
|
|
|
data.aws_availability_zones.available: Still destroying... [id=2017-03-05 10:56:59.298784526 +0000 UTC, 1s elapsed]
|
|
|
|
data.aws_availability_zones.available: Still destroying... [id=2017-03-05 10:56:59.298784526 +0000 UTC, 2s elapsed]
|
|
|
|
data.aws_availability_zones.available: Still destroying... [id=2017-03-05 10:56:59.298784526 +0000 UTC, 3s elapsed]
|
2017-03-13 15:09:25 -05:00
|
|
|
`
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
if output != expectedOutput {
|
|
|
|
t.Fatalf("Output didn't match.\nExpected: %q\nGiven: %q", expectedOutput, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedErrOutput := ""
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if errOutput != expectedErrOutput {
|
|
|
|
t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUiHookPreApply_destroy(t *testing.T) {
|
2017-08-14 10:19:40 -05:00
|
|
|
ui := cli.NewMockUi()
|
2017-03-13 15:09:25 -05:00
|
|
|
h := &UiHook{
|
|
|
|
Colorize: &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
|
|
|
Disable: true,
|
|
|
|
Reset: true,
|
|
|
|
},
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
h.init()
|
|
|
|
h.resources = map[string]uiResourceState{
|
|
|
|
"data.aws_availability_zones.available": uiResourceState{
|
|
|
|
Op: uiResourceDestroy,
|
|
|
|
Start: time.Now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
addr := addrs.Resource{
|
|
|
|
Mode: addrs.DataResourceMode,
|
|
|
|
Type: "aws_availability_zones",
|
|
|
|
Name: "available",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
2018-10-13 12:47:46 -05:00
|
|
|
priorState := cty.ObjectVal(map[string]cty.Value{
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
"id": cty.StringVal("2017-03-05 10:56:59.298784526 +0000 UTC"),
|
|
|
|
"names": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("us-east-1a"),
|
|
|
|
cty.StringVal("us-east-1b"),
|
|
|
|
cty.StringVal("us-east-1c"),
|
|
|
|
cty.StringVal("us-east-1d"),
|
|
|
|
}),
|
|
|
|
})
|
2018-10-13 12:47:46 -05:00
|
|
|
plannedNewState := cty.NullVal(cty.Object(map[string]cty.Type{
|
|
|
|
"id": cty.String,
|
|
|
|
"names": cty.List(cty.String),
|
|
|
|
}))
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
|
|
|
|
action, err := h.PreApply(addr, states.CurrentGen, plans.Delete, priorState, plannedNewState)
|
2017-03-13 15:09:25 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
// stop the background writer
|
|
|
|
uiState := h.resources[addr.String()]
|
|
|
|
close(uiState.DoneCh)
|
|
|
|
<-uiState.done
|
|
|
|
|
2018-10-13 12:47:46 -05:00
|
|
|
expectedOutput := "data.aws_availability_zones.available: Destroying... [id=2017-03-05 10:56:59.298784526 +0000 UTC]\n"
|
2017-03-13 15:09:25 -05:00
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
if output != expectedOutput {
|
|
|
|
t.Fatalf("Output didn't match.\nExpected: %q\nGiven: %q", expectedOutput, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedErrOutput := ""
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if errOutput != expectedErrOutput {
|
|
|
|
t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUiHookPostApply_emptyState(t *testing.T) {
|
2017-08-14 10:19:40 -05:00
|
|
|
ui := cli.NewMockUi()
|
2017-03-13 15:09:25 -05:00
|
|
|
h := &UiHook{
|
|
|
|
Colorize: &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
|
|
|
Disable: true,
|
|
|
|
Reset: true,
|
|
|
|
},
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
h.init()
|
|
|
|
h.resources = map[string]uiResourceState{
|
|
|
|
"data.google_compute_zones.available": uiResourceState{
|
|
|
|
Op: uiResourceDestroy,
|
|
|
|
Start: time.Now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
addr := addrs.Resource{
|
|
|
|
Mode: addrs.DataResourceMode,
|
|
|
|
Type: "google_compute_zones",
|
|
|
|
Name: "available",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
newState := cty.NullVal(cty.Object(map[string]cty.Type{
|
|
|
|
"id": cty.String,
|
|
|
|
"names": cty.List(cty.String),
|
|
|
|
}))
|
|
|
|
|
|
|
|
action, err := h.PostApply(addr, states.CurrentGen, newState, nil)
|
2017-03-13 15:09:25 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
}
|
|
|
|
|
2019-01-31 06:25:57 -06:00
|
|
|
expectedRegexp := "^data.google_compute_zones.available: Destruction complete after -?[a-z0-9µ.]+\n$"
|
2017-03-13 15:09:25 -05:00
|
|
|
output := ui.OutputWriter.String()
|
2017-07-13 15:01:19 -05:00
|
|
|
if matched, _ := regexp.MatchString(expectedRegexp, output); !matched {
|
|
|
|
t.Fatalf("Output didn't match regexp.\nExpected: %q\nGiven: %q", expectedRegexp, output)
|
2017-03-13 15:09:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedErrOutput := ""
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if errOutput != expectedErrOutput {
|
|
|
|
t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 16:16:22 -06:00
|
|
|
func TestTruncateId(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
Input string
|
|
|
|
Expected string
|
|
|
|
MaxLen int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: "Hello world",
|
|
|
|
Expected: "H...d",
|
|
|
|
MaxLen: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "Hello world",
|
|
|
|
Expected: "H...d",
|
|
|
|
MaxLen: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "Hello world",
|
|
|
|
Expected: "He...d",
|
|
|
|
MaxLen: 6,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "Hello world",
|
|
|
|
Expected: "He...ld",
|
|
|
|
MaxLen: 7,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "Hello world",
|
|
|
|
Expected: "Hel...ld",
|
|
|
|
MaxLen: 8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "Hello world",
|
|
|
|
Expected: "Hel...rld",
|
|
|
|
MaxLen: 9,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "Hello world",
|
|
|
|
Expected: "Hell...rld",
|
|
|
|
MaxLen: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "Hello world",
|
|
|
|
Expected: "Hello world",
|
|
|
|
MaxLen: 11,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "Hello world",
|
|
|
|
Expected: "Hello world",
|
|
|
|
MaxLen: 12,
|
|
|
|
},
|
2018-09-09 10:21:24 -05:00
|
|
|
{
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
Expected: "あ...さ",
|
|
|
|
MaxLen: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
Expected: "あ...さ",
|
|
|
|
MaxLen: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
Expected: "あい...さ",
|
|
|
|
MaxLen: 6,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
Expected: "あい...こさ",
|
|
|
|
MaxLen: 7,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
Expected: "あいう...こさ",
|
|
|
|
MaxLen: 8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
Expected: "あいう...けこさ",
|
|
|
|
MaxLen: 9,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
Expected: "あいうえ...けこさ",
|
|
|
|
MaxLen: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
Expected: "あいうえおかきくけこさ",
|
|
|
|
MaxLen: 11,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
Expected: "あいうえおかきくけこさ",
|
|
|
|
MaxLen: 12,
|
|
|
|
},
|
2017-03-01 16:16:22 -06:00
|
|
|
}
|
|
|
|
for i, tc := range testCases {
|
|
|
|
testName := fmt.Sprintf("%d", i)
|
|
|
|
t.Run(testName, func(t *testing.T) {
|
|
|
|
out := truncateId(tc.Input, tc.MaxLen)
|
|
|
|
if out != tc.Expected {
|
|
|
|
t.Fatalf("Expected %q to be shortened to %d as %q (given: %q)",
|
|
|
|
tc.Input, tc.MaxLen, tc.Expected, out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|