public void TemplateAsync_WithAwaitCalls_MayBeAsync() { AsyncStringTemplates t = new AsyncStringTemplates() { MayBeAsync = true }; string template = $@"{t.MaybeAsyncAndNot($@"public {t.MaybeAsyncReturnType("int")} MyMethod{t.MaybeAsyncWordAsync()}() {{ {t.MaybeAsyncWordAwait()}MyOtherMethod{t.MaybeAsyncWordAsync()}(); return {t.MaybeAsyncReturnValue("3")}; }}")}"; string result = t.Process(template, new Dictionary <string, string>()); string expected = $@"public int MyMethod() {{ MyOtherMethod(); return 3; }} async public ValueTask<int> MyMethodAsync() {{ await MyOtherMethodAsync(); return 3; }} "; result.Should().Be(expected); }
public void TemplateAsync_WithoutAwaitCalls_MayNotBeAsync() { AsyncStringTemplates t = new AsyncStringTemplates() { MayBeAsync = false }; string template = $"{t.MaybeAsyncAndNot($@"public {t.MaybeAsyncReturnType("int")} MyMethod() => {t.MaybeAsyncReturnValue("3")};")}"; string result = t.Process(template, new Dictionary <string, string>()); string expected = $@"public int MyMethod() => 3;"; result.Should().Be(expected); }
public void TemplateAsync_CanManuallyConstructMaybeAsyncBlock() { AsyncStringTemplates t = new AsyncStringTemplates() { MayBeAsync = true }; string template1 = $@"{t.MaybeAsyncAndNot($@"public {t.MaybeAsyncReturnType("int")} MyMethod{t.MaybeAsyncWordAsync()}() {{ {t.MaybeAsyncWordAwait()}MyOtherMethod{t.MaybeAsyncWordAsync()}(); return {t.MaybeAsyncReturnValue("3")}; }}")}"; string template2 = $@"{t.MaybeAsyncAndNot_Begin}public {t.MaybeAsyncReturnType("int")} MyMethod{t.MaybeAsyncWordAsync()}() {{ {t.MaybeAsyncWordAwait()}MyOtherMethod{t.MaybeAsyncWordAsync()}(); return {t.MaybeAsyncReturnValue("3")}; }}{t.MaybeAsyncAndNot_End}"; template2.Should().Be(template1); }