1 /* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.facebook.ktfmt.onlineformatter 18 19 import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent 20 import kotlin.test.assertEquals 21 import org.junit.Test 22 import org.junit.runner.RunWith 23 import org.junit.runners.JUnit4 24 25 @RunWith(JUnit4::class) 26 class HandlerTest { 27 28 @Test dontCrash_successnull29 fun dontCrash_success() { 30 val event = APIGatewayProxyRequestEvent() 31 event.body = """{"source": "fun foo ( ) { }","style": "--kotlinlang-style"}""" 32 val response = Handler().handleRequest(event, null) 33 assertEquals("""{"source":"fun foo() {}\n"}""", response) 34 } 35 36 @Test dontCrash_malformedRequestJsonnull37 fun dontCrash_malformedRequestJson() { 38 val event = APIGatewayProxyRequestEvent() 39 event.body = """{"source": "fun foo ( ) { }"style": "--kotlinlang-style"}""" 40 val response = Handler().handleRequest(event, null) 41 assertEquals( 42 """{"err":"com.google.gson.stream.MalformedJsonException: """ + 43 """Unterminated object at line 1 column 30 path ${'$'}.source"}""", 44 response) 45 } 46 47 @Test dontCrash_emptyRequestJsonnull48 fun dontCrash_emptyRequestJson() { 49 val event = APIGatewayProxyRequestEvent() 50 event.body = "" 51 val response = Handler().handleRequest(event, null) 52 assertEquals("{}", response) 53 } 54 55 @Test dontCrash_malformedSourceCodenull56 fun dontCrash_malformedSourceCode() { 57 val event = APIGatewayProxyRequestEvent() 58 event.body = """{"source": "fun foo ( { }","style": "--kotlinlang-style"}""" 59 val response = Handler().handleRequest(event, null) 60 assertEquals("""{"err":"1:10: error: Expecting \u0027)\u0027"}""", response) 61 } 62 63 @Test dontCrash_allNullRequestnull64 fun dontCrash_allNullRequest() { 65 val event = APIGatewayProxyRequestEvent() 66 event.body = """{}""" 67 val response = Handler().handleRequest(event, null) 68 assertEquals("""{"source":"\n"}""", response) 69 } 70 71 @Test dontCrash_malformedStylenull72 fun dontCrash_malformedStyle() { 73 val event = APIGatewayProxyRequestEvent() 74 event.body = """{"source": "fun foo ( ) { }","style": "blabla"}""" 75 val response = Handler().handleRequest(event, null) 76 assertEquals("""{"source":"fun foo() {}\n"}""", response) 77 } 78 79 @Test dontCrash_noStylenull80 fun dontCrash_noStyle() { 81 val event = APIGatewayProxyRequestEvent() 82 event.body = """{"source": "fun foo ( ) { }"}""" 83 val response = Handler().handleRequest(event, null) 84 assertEquals("""{"source":"fun foo() {}\n"}""", response) 85 } 86 87 @Test dontCrash_negativeMaxWidthnull88 fun dontCrash_negativeMaxWidth() { 89 val event = APIGatewayProxyRequestEvent() 90 event.body = """{"source": "fun foo ( ) { }", "maxWidth": -100}""" 91 val response = Handler().handleRequest(event, null) 92 assertEquals("""{"source":"fun foo() {}\n"}""", response) 93 } 94 95 /** maxWidth == null is treated as if it were 100 */ 96 @Test dontCrash_nullMaxWidthnull97 fun dontCrash_nullMaxWidth() { 98 val event = APIGatewayProxyRequestEvent() 99 event.body = """{"source": "fun foo ( ) { }", "maxWidth": null}""" 100 val response = Handler().handleRequest(event, null) 101 assertEquals("""{"source":"fun foo() {}\n"}""", response) 102 } 103 104 @Test dontCrash_nonIntegerMaxWidthnull105 fun dontCrash_nonIntegerMaxWidth() { 106 val event = APIGatewayProxyRequestEvent() 107 event.body = """{"source": "fun foo ( ) { }", "maxWidth": "bla"}""" 108 val response = Handler().handleRequest(event, null) 109 assertEquals( 110 """{"err":"java.lang.NumberFormatException: For input string: \"bla\""}""", response) 111 } 112 113 @Test dontCrash_coerceMaxWidthToIntnull114 fun dontCrash_coerceMaxWidthToInt() { 115 val event = APIGatewayProxyRequestEvent() 116 event.body = """{"source": "fun foo ( ) { }", "maxWidth": "100"}""" 117 val response = Handler().handleRequest(event, null) 118 assertEquals("""{"source":"fun foo() {}\n"}""", response) 119 } 120 } 121