|
| 1 | +#![cfg(test)] |
| 2 | +use super::*; |
| 3 | +use crate::test::{test_match_lang,test_replace_lang}; |
| 4 | + |
| 5 | +fn test_match(s1: &str, s2: &str) { |
| 6 | + test_match_lang(s1, s2, Hcl) |
| 7 | +} |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn test_hcl_pattern() { |
| 11 | + test_match("$A = $B", "foo = \"bar\""); |
| 12 | + test_match("resource $TYPE $NAME $BODY", "resource \"aws_instance\" \"example\" { ami = \"ami-123\" }"); |
| 13 | + test_match("$BLOCK $BODY", "terraform { required_providers { aws = { source = \"hashicorp/aws\" } } }"); |
| 14 | + test_match("variable $NAME $CONFIG", "variable \"region\" { default = \"us-west-2\" }"); |
| 15 | + test_match("output $NAME $VALUE", "output \"instance_ip\" { value = aws_instance.example.public_ip }"); |
| 16 | + test_match("$VAR = [$$$ITEMS]", "tags = [\"production\", \"web\"]"); |
| 17 | + test_match("$VAR = { $$$PAIRS }", "labels = { environment = \"prod\", team = \"backend\" }"); |
| 18 | + test_match("$VAR = \"$CONTENT\"", "name = \"instance\""); |
| 19 | +} |
| 20 | + |
| 21 | +fn test_replace(src: &str, pattern: &str, replacer: &str) -> String { |
| 22 | + test_replace_lang(src, pattern, replacer, Hcl) |
| 23 | +} |
| 24 | + |
| 25 | +#[test] |
| 26 | +fn test_hcl_replace() { |
| 27 | + let ret = test_replace( |
| 28 | + "foo = \"bar\"", |
| 29 | + "$A = $B", |
| 30 | + "$B = $A" |
| 31 | + ); |
| 32 | + assert_eq!(ret, "\"bar\" = foo"); |
| 33 | + |
| 34 | + let ret = test_replace( |
| 35 | + "resource \"aws_instance\" \"example\" { ami = \"ami-123\" }", |
| 36 | + "resource $TYPE $NAME $BODY", |
| 37 | + "resource $NAME $TYPE $BODY", |
| 38 | + ); |
| 39 | + assert_eq!(ret, "resource \"example\" \"aws_instance\" { ami = \"ami-123\" }"); |
| 40 | + |
| 41 | + let ret = test_replace( |
| 42 | + "variable \"region\" { default = \"us-west-2\" }", |
| 43 | + "variable \"region\" { default = $DEFAULT }", |
| 44 | + "variable \"region\" { default = \"eu-west-1\" }", |
| 45 | + ); |
| 46 | + assert_eq!(ret, "variable \"region\" { default = \"eu-west-1\" }"); |
| 47 | +} |
0 commit comments