import org.joda.time.LocalDate
import static org.joda.time.Days.*
date1 = new LocalDate(2008, 3, 10)
date2 = new LocalDate(2008, 3, 12)
assert 2 == daysBetween(date1, date2).days
What's not to like? :o)
import org.joda.time.LocalDate
import static org.joda.time.Days.*
date1 = new LocalDate(2008, 3, 10)
date2 = new LocalDate(2008, 3, 12)
assert 2 == daysBetween(date1, date2).days
void testUpdateNotFound() {
def bc
def mock = new MockFor(BookmarkController)
mock.demand.redirect { Map params ->
assert params.action == bc.edit
}
mock.use {
bc = new BookmarkController()
bc.params.id = 5
bc.update.call()
}
}import groovy.mock.interceptor.*
class BookmarkController {
def update = { redirect("value from original") }
def redirect = { println "Original class: $it" }
}
class BookmarkTests extends GroovyTestCase {
void testUpdate() {
def bc
def mock = new MockFor(BookmarkController)
mock.demand.redirect { println "Mock class: $it" }
mock.use {
bc = new BookmarkController()
bc.&update.call()
}
}
}
new BookmarkController().&update.call()
new BookmarkTests().testUpdate()
On two occasions I have been asked, – "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" In one case a member of the Upper, and in the other a member of the Lower, House put this question. I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
-- Charles Babbage, English mathematician, philosopher, and mechanical engineer
#! /usr/bin/groovy
df = new java.text.SimpleDateFormat('yyyy-MM-dd')
// These dates cross DST...
startDate = df.parse('2007-03-10')
endDate = df.parse('2007-03-12')
expectedDifferenceInDays = 2
// Here's the problem with millis:
millisInADay = 1000 * 60 * 60 * 24
differenceInMillis = endDate.time - startDate.time
actualDifferenceInDays = differenceInMillis / millisInADay
assert expectedDifferenceInDays != actualDifferenceInDays
assert actualDifferenceInDays < expectedDifferenceInDays
#! /usr/bin/groovy
df = new java.text.SimpleDateFormat('yyyy-MM-dd')
// These dates cross DST...
startDate = df.parse('2007-03-10')
endDate = df.parse('2007-03-12')
expectedDifferenceInDays = 2
calendar = Calendar.instance
calendar.time = startDate
actualDifferenceInDays = 0
while (calendar.time.before(endDate)) {
actualDifferenceInDays++
calendar.add(Calendar.DATE, 1)
}
assert expectedDifferenceInDays == actualDifferenceInDays
using System.Diagnostics;
class ScratchUtils
{
public static int Reverse(int i)
{
int result = 0;
while (i != 0)
{
result = (result * 10) + (i % 10);
i /= 10;
}
return result;
}
public static bool IsPalindrome(int i)
{
return i == Reverse(i);
}
public static void Main()
{
Debug.Assert( 0 == Reverse(0));
Debug.Assert( 1 == Reverse(1));
Debug.Assert( 21 == Reverse(12));
Debug.Assert(-21 == Reverse(-12));
Debug.Assert(IsPalindrome(1));
Debug.Assert(IsPalindrome(11));
Debug.Assert(!IsPalindrome(12));
Debug.Assert(IsPalindrome(121));
}
}
There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="Data Source=localhost;
Initial Catalog=aspnetdb;
Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Half the nation’s families earn below the median family income of about $56,000.
sub add {
my $x = shift;
my $f = sub {
my $y = shift;
return $x + $y;
}
return $f;
} def add (x) {
return {
y -> return x + y
}
}assert 7 == add(3)(4) // whew.
non-static variable cannot be referenced from a static contextswitch(s) {
case "Foo": doSomething(); break;
case "Bar": doSomethingElse(); break;
}The short answer is that one can't. In Java (versions before 5), the expression evaluated in the switch statement must be a char, byte, short, or int. With auto-unboxing and the new enum capabilities of Java 5 and later versions, switch expression may also include Character, Byte, Short, Integer, or enum types. Furthermore, every case expression must be assignable to the same type as what is declared in the switch statement. This means that if your switch statement is an int type, your case statements can be int types, byte types, short types, etc. If, on the other hand, your switch statement is a byte type, the compiler will bark if you try to use an int type in a case expression. In short, however, switch statements with String types (and any type not mentioned above) are simply not available in Java.