Vim, Emacs, and JUnit Testing Audience: Students in CS 331

Transcription

Vim, Emacs, and JUnit Testing Audience: Students in CS 331
Vim, Emacs, and
JUnit Testing
Audience: Students in CS 331
Written by: Kathleen Lockhart, CS Tutor
Overview
• Vim and Emacs are the two code editors available within the
Dijkstra environment. While both perform essentially the
same function, each has it own characteristics, advantages,
and disadvantages.
• JUnit tests allow you to test how your code runs under various
situations. Using JUnit is required for CS 331.
Vim
• Standing for “vi improved” (vi is a text editor for Unix
systems), Vim was released in 1991
• Vim is a free and open source software
• Vim is entirely a text editor, it has no graphics whatsoever.
Graphic versions of Vim do exist, but will not be covered in
this tutorial
Vim Tutor
• Vim has a tutor to teach you many of the commands that can
be used in Vim.
• Vim tutor contains many additional commands not covered in
this workshop. This workshop focuses on the commands most
commonly used in CS 331.
• To use the tutor, type vimtutor and press enter. Note that you
can launch the Vim tutor from within any directory in Dijkstra.
Launching Vim
• Editing an existing file
• Type vim followed by the program name and hit enter
• Example: vim Stack.java
• Creating a new file
• Similar to editing an existing file, type vim followed by the desired
program name and hit enter
• Example: vim Stack2.java
Vim Navigation
• Vim is completely a text editor, so it will not respond to any
input in the mouse. You cannot click to a location in your code,
Vim will simply ignore it.
• To navigate, use either the arrow keys or the h, j, k, and l keys
•
•
•
•
h is  (left)
j is ↓ (down)
k is ↑ (up)
l is  (right)
Vim File Commands
Vim has a few basic file commands. These commands are all
executed preceded by a : and press enter after the command
• w: save file
• q: close file
• The q command will only work when you have not made any
changes to the file since you saved it. To close a file you have made
changes to, use q! or wq
• q!: close file without saving
• wq: save and close file
Vim File Commands Practice
•
•
•
•
•
Open the Stack.java file (or any other file)
Type “:w” and press enter. This saves the file.
Type “:q!” and press enter. This closes the file.
Open the Stack.java file again
Type “:wq” and press enter. This saves and closes the file in
one line.
Vim Insertion Mode
• To actually edit files, Vim has a special mode called the
insertion mode.
• Switch to insertion mode by pressing i
• Switch out of insertion mode by pressing the escape key
Editing in Vim
• Navigation in insert mode is the same as before: use the arrow
keys or h,j,k, and l keys to navigate through your code
• Typed text will appear directly to the left of the cursor,
likewise pressing either backspace or delete will delete the
character to the left of the cursor
Vim Highlighting Features
•
•
•
•
Keywords such as public, int, this, class, etc. show up in green
Variable values (ex: 0, null, “hi”) are shown in red
New is yellow
Comments are blue
Vim Editing Features
• Move your cursor to the right of a curly bracket to have the
matching one be highlighted blue
• Vim does not automatically indent your code, however, if you
are typing in an indented line, hitting enter will indent the
next line by the same amount
Vim Editing Practice
Try creating a simple HelloWorld file using the techniques and
features discussed. Navigate through the file to see how Vim
keeps track of matching pairs of brackets.
Copy and Paste in Vim
• Before you try to copy and paste, exit insertion mode.
• Select the text you want to copy by moving the cursor to the
beginning of the text, pressing v (visual), and then using the
arrow keys to highlight the desired text
• Copy this text using y (yank) (or press d to cut)
• Move to where you want to paste using the arrow keys and
press p (paste)
Vim Copy and Paste Practice
Copy the main method from the HelloWorld file from the
earlier example and paste it into a new java file. Save and
close both files.
Vim Command Summary
• Vim FileName.java – Open or create a file to
edit
• :w + Enter – save file
• :q + Enter – quit, close file
• :q! + Enter – close file without saving
•
•
•
•
•
•
:wq + Enter – save and close file
i – start insertion mode
v – visual, select a section of text
y – yank, copy
d – delete, cut
p - paste
Emacs
• Emacs was first released 1976 as a set of Editor MACroS for
the TECO editor
• Emacs is a free and open source software
• Emacs is entirely a text editor, it has no graphics whatsoever.
Graphic versions of Emacs do exist, but will not be covered in
this tutorial
Launching Emacs
• Editing an existing file
• Type emacs followed by the program name and hit enter
• Example: emacs Stack.java
• Creating a new file
• Similar to editing an existing file, type emacs followed by the
desired program name and hit enter
• Example: emacs Stack2.java
Emacs Tutor
• Emacs has a tutor to teach you many of the commands that
can be used in Emacs.
• Emacs tutor contains many additional commands not covered
in this workshop. This workshop focuses on the commands
most commonly used in CS 331.
• To use the tutor launch Emacs by typing “emacs” and pressing
enter
• Press the control (Ctrl) key and h, followed by t to launch the
tutorial
Navigation in Emacs
• Emacs is completely a text editor, so it will not respond to any
input in the mouse. You cannot click to a location in your code,
Vim will simply ignore it.
• Navigate in Emacs using the arrow keys
• You can also use the Ctrl key + p, b, f, and n to navigate
•
•
•
•
p previous (line above)
n next (line below)
f forward (to the right)
b back (to the left)
Emacs File Commands
• To close a file, press Ctrl X, followed by Ctrl C
• Emacs will ask you if you want to save. Press y to save, n to not
save, or Ctrl + h for help
• If you say no, then Emacs will ask you again. This time you must
type either yes or no
• To save a file, press Ctrl X, followed by Ctrl S
Emacs File Commands Practice
•
•
•
•
Open the Stack.java file (or any other file)
Type Ctrl + X, followed by Ctrl + S. This saves the file.
Type Ctrl + X, followed by Ctrl + C. This closes the file.
Choose whether or not to save your file when prompted
Editing in Emacs
• Unlike Vim, emacs has only one mode (i.e. there is no
separate insertion mode), so whatever you type will appear in
the file
• Typed text will appear directly to the left of the cursor,
likewise pressing either backspace will delete the character to
the left of the cursor
• Note that pressing delete does not delete characters you have
typed. You must use backspace
Emacs Highlighting
• Keywords such as public, this, class, new, etc. show up in light
blue
• Variable types are shown in green
• Comments are shown in red
• Variable names are yellow
• Null and external classes are purple
• Method names are blue
• Note that method names are only in blue for classes not using
generics (<E>, etc.)
• Method names in classes using generics are in yellow
• Constructors are not highlighted
Emacs Editing Features
• When you type a closing curly bracket, Emacs will briefly move
the cursor to the opening curly bracket matching it
• Emacs does not automatically indent your code, however, if
you are typing in an indented line, hitting enter will indent the
next line by the same amount
Emacs Editing Practice
Try creating a simple HelloWorld file using the techniques and
features discussed. Try adding lines of the code to the file to
explore all of the editing features of Emacs
Copy and Paste in Emacs
• To Copy:
• Press Ctrl space
• Move the cursor until all the text you want is highlighted
• When you highlight this text, you must include one extra character at
the end or Emacs will cut off the final character
• Then press Esc , followed by w
Copy and Paste in Emacs
• To Paste:
• To paste press Ctrl + y
• The text will appear after the cursor
Emacs Copy and Paste Practice
Copy the main method from the HelloWorld file from the
earlier example and paste it into a new java file. Save and
close both files.
Emacs Command Summary
• Emacs FileName – Open or create a file to edit
• Ctrl + X, Ctrl + C – Close a file. Use menu options to decide
whether or not to save
• Ctrl + X, Ctrl + S – Save a file
• Ctrl + Space – Start copy
• Esc + w – End copy
• Ctrl + Y - Paste
JUnit
• JUnit, or java unit, tests allow you to test how your code runs
under various situations
• Unit tests get their own java file, usually called TestFile.java (so
unit tests for Stack.java would be in TestStack.java)
JUnit File Setup
• The building blocks of every JUnit class:
• Import statement:
import junit.framework.*;
• Class declaration:
public class TestFile extends TestCase{
• Default constructor:
public TestFile(String name) {
super(name);
}
JUnit File Setup Example
TestStack.java
Declaring variables at the top of the file is usually helpful as it
allows you to use them in all of your unit tests.
JUnit Methods
• All of your methods should begin with “test”, otherwise JUnit
will not run them. The return type should be void and the
methods should take no arguments
• Example: public void testEmpty() {
• As with most other Java files, you can create variables,
including instances of the class you are testing within your
methods, and call methods on those variables
JUnit – assertEquals
• The assertEquals method will run a method from the class you
are testing, and compare it against the expected return value
of that method (that you provide)
• Arguments (in order):
• String: the method label, something describing what you are
testing
• String, int, double, etc.: the expected return value of the method
being tested
• Method: the method you want to test
JUnit – assertEquals Example
Running JUnit Tests
• Compile your main
class and your test
class normally
• Run your test class by
typing “scons tests”
and hitting enter
• If all your tests pass
the output will look
something like this:
JUnit Test Errors
•
•
•
•
Red: Class where test failed
Orange: Method where test failed
Green: Label and expected and actual values of failed test
Blue: Exact location where failure occurred
(file:line number)
• By reading the top line: An error occurred in the testEmpty
method of the TestStack class. The Pop Test failed. The expected
return value was 0, but the actual return value was -1.
Questions?