Skip to content

Commit

Permalink
Simple example application to demonstrate testing launchpad paramter …
Browse files Browse the repository at this point in the history
…conversion
  • Loading branch information
macta committed May 7, 2024
1 parent 2c3c1b5 commit 44cbfc7
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"
A LaunchpadCommandLineHandlerTest is a test class for testing the behavior of LaunchpadCommandLineHandler
"
Class {
#name : 'LaunchpadZnHelloApplicationTest',
#superclass : 'LaunchpadTest',
#category : 'Launchpad-Commands-Tests',
#package : 'Launchpad-Commands-Tests'
}

{ #category : 'tests' }
LaunchpadZnHelloApplicationTest >> testConfigParsingInValidLogLevel [
"Verify the launchpad handler properly handles non numeric input"

| application mockContext config |

"Test passing a non-numeric log level parameter"
config := NullConfigurationProvider new
atName: #'log-level' putValue: 'false';
yourself.

application := LaunchpadZnHelloApplication
runningIn: DebuggingApplicationMode new
configuredBy: config
controlledBy: NullCommandServer new.

"Expect the Launchpad application to handle the error and substitute a loglevel of 0"
mockContext := MockObject new
on: #runZnHelloWithPort:debugLevel:
with: 8181
with: 0;
yourself.

application basicStartWithin: mockContext.

mockContext verifyIn: self
]

{ #category : 'tests' }
LaunchpadZnHelloApplicationTest >> testConfigParsingValidLogLevel [
"Verify the launchpad handler correctly parses the paramter to an integer"

| application mockContext config |

config := NullConfigurationProvider new
atName: #'log-level' putValue: '2';
yourself.

application := LaunchpadZnHelloApplication
runningIn: DebuggingApplicationMode new
configuredBy: config
controlledBy: NullCommandServer new.

mockContext := MockObject new
on: #runZnHelloWithPort:debugLevel:
with: 8181
with: 2;
yourself.

application basicStartWithin: mockContext.

mockContext verifyIn: self
]

{ #category : 'tests' }
LaunchpadZnHelloApplicationTest >> testDryRunApplication [

self
should: [
LaunchpadCommandLineHandler activateWithArguments:
#( 'launchpad' 'start' '--dry-run' 'znHello' )
]
raise: Exit
withExceptionDo: [ :exit | self assert: exit isSuccess ]

]
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@ LaunchpadCommandLineHandler >> activateWithContext: context [
StandardStreamLogger onStandardError startFor:
( LogRecord where: [ :record | record isInformational not ] ).

LogRecord emitInfo: 'Launchpad processing command line parameters'.

^ LaunchpadRootCommand new evaluateWithin: context
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Extension { #name : 'LaunchpadApplicationContext' }

{ #category : '*Launchpad-Examples' }
LaunchpadApplicationContext >> runZnHelloWithPort: portNumber debugLevel: debugLevel [
"Context extension that allows easy launchpad parameter testing"

LaunchpadZnHelloApplication startServerOn: portNumber debug: debugLevel
]
100 changes: 100 additions & 0 deletions source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"
Small example Zinc server application to demonstrate Launchpad with tiny web server example
"
Class {
#name : 'LaunchpadZnHelloApplication',
#superclass : 'LaunchpadApplication',
#category : 'Launchpad-Examples',
#package : 'Launchpad-Examples'
}

{ #category : 'accessing' }
LaunchpadZnHelloApplication class >> commandName [

^ 'znHello'
]

{ #category : 'private - accessing' }
LaunchpadZnHelloApplication class >> configurationParameters [

^ {
(OptionalConfigurationParameter
named: #'log-level'
describedBy: 'Log http requests level'
defaultingTo: 0).

(OptionalConfigurationParameter
named: #port
describedBy: 'Server port'
defaultingTo: 8181) }
]

{ #category : 'accessing' }
LaunchpadZnHelloApplication class >> description [

^ 'Launchpad demo zinc http application'
]

{ #category : 'example' }
LaunchpadZnHelloApplication class >> startServer [
<script>
"This would really be in its own server class, but here to demonstrate a simple example of a running application to start via the command line.
eg using: launchpad start ZnHello --log-level=2"

self startServerOn: 8181 debug: true
]

Check warning on line 45 in source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st

View check run for this annotation

Codecov / codecov/patch

source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st#L39-L45

Added lines #L39 - L45 were not covered by tests

{ #category : 'example' }
LaunchpadZnHelloApplication class >> startServerOn: port debug: debug [
"This would really be in its own server class, but here to demonstrate a simple example"

| server |
server := (ZnServer defaultOn: port).
debug ifTrue: [ server logToTranscript ].
server start.
]

Check warning on line 55 in source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st

View check run for this annotation

Codecov / codecov/patch

source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st#L48-L55

Added lines #L48 - L55 were not covered by tests

{ #category : 'example' }
LaunchpadZnHelloApplication class >> stopServer [
<script>
"This would really be in its own server class, but here to demonstrate a simple example"

ZnServer default stop
]

Check warning on line 63 in source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st

View check run for this annotation

Codecov / codecov/patch

source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st#L58-L63

Added lines #L58 - L63 were not covered by tests

{ #category : 'accessing' }
LaunchpadZnHelloApplication class >> version [

^ 'v1.0.0'
]

{ #category : 'private - activation' }
LaunchpadZnHelloApplication >> basicStartWithin: context [
"This is an example of a launcher using a testable #run:withArguments: pattern. See the example test "

| debugLevel serverPort |

debugLevel := [(self configuration valueAt: #'log-level') asNumber] on: Error do: [ 0 ].
serverPort := (self configuration valueAt: #port) asInteger.

"This would be something to call for your own application class to allow testing"
context runZnHelloWithPort: serverPort debugLevel: debugLevel
]

{ #category : 'accessing' }
LaunchpadZnHelloApplication >> name [

^ self configuration name
]

Check warning on line 88 in source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st

View check run for this annotation

Codecov / codecov/patch

source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st#L85-L88

Added lines #L85 - L88 were not covered by tests

{ #category : 'error handling' }
LaunchpadZnHelloApplication >> stackTraceDumper [

^ NullStackTraceDumper new
]

Check warning on line 94 in source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st

View check run for this annotation

Codecov / codecov/patch

source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st#L91-L94

Added lines #L91 - L94 were not covered by tests

{ #category : 'accessing' }
LaunchpadZnHelloApplication >> title [

^ self configuration title
]

Check warning on line 100 in source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st

View check run for this annotation

Codecov / codecov/patch

source/Launchpad-Examples/LaunchpadZnHelloApplication.class.st#L97-L100

Added lines #L97 - L100 were not covered by tests

0 comments on commit 44cbfc7

Please sign in to comment.