9

Is there a way to compare two text files and show the diff if they are not identical in dunit?

The easy start is to read them to TStringList, however the code for comparing two text file is much more complicated, and the gui in the DUnitGui is not sufficient for this.

Any idea? suggestion?

none
  • 4,669
  • 14
  • 62
  • 102
  • is it written in delphi? – none Mar 01 '11 at 10:37
  • @none No, you'd have to shell out to it, create a process, wait for it to complete, and capture stdout. I'm sure you could link it in programatically too it's just that the hacky way is easier. – David Heffernan Mar 01 '11 at 10:39
  • Yes, DUnit comes with source code. There are diff libraries available, for example Subversion has diff APIs. – Ondrej Kelle Mar 01 '11 at 10:40
  • 1
    @TOndrej "Subversion has diff APIs" Really? I'd like to know more about that. – David Heffernan Mar 01 '11 at 10:41
  • 2
    @David Heffernan: Subversion diff APIs: http://subversion.apache.org/docs/api/1.6/svn__diff_8h.html – Ondrej Kelle Mar 01 '11 at 10:44
  • 1
    @David Heffernan: example usage: http://delphisvn.svn.sourceforge.net/viewvc/delphisvn/trunk/src/SvnDiff3Frame.pas?revision=69&view=markup – Ondrej Kelle Mar 01 '11 at 10:46
  • 1
    @TOndrej Thanks. I'm actually looking to put a very simple diff engine into my program. This might be useful. – David Heffernan Mar 01 '11 at 10:47
  • Just wanted to point out that its usually best to avoid writing to the file system in unit tests. Tests that access the file system are 50 to 100 times slower than tests that run in memory. Worse still it introduces the temptation use the artifacts from one test as the input for another, leading to brittle or erratic test behavior. What are you testing that is complex enough to require a differencing engine to find the cause of failures? – Kenneth Cochran Mar 03 '11 at 20:43
  • @codeelegance I test conversion of Binary file to Text file, and want to use the oracle to see if the code runs correctly. – none Mar 06 '11 at 11:34

1 Answers1

9

There is a nice little unit that comes with some examples called TDiff, this is available from http://angusj.com/delphi/ and will allow you to compare 2 files and see the differences, it also allows for merging.

It is a very simple Utility that you can download the entire source for.

Reallyethical
  • 1,825
  • 2
  • 16
  • 26