Пример #1
0
    def Diff(self):

        #Validate if the Diff may proceed (Both JSONs must be valid)
        if self.isLeftValid() == "false":
            return "Left is not a valid JSON"
        elif self.isRightValid() == "false":
            return "Right is not a valid JSON"

        diffInstance = difflib.Differ()
        diffList = list(diffInstance.compare(self.decodedleft.splitlines(), self.decodedright.splitlines()))

        bytesread = 0
        count = 1

        #Create a new Diff Result
        #Set decoded values for comparison laters
        diffresult = DiffResult()
        diffresult.decodedleft = self.decodedleft
        diffresult.decodedright = self.decodedright

        #only compare offsets if contents are different and size is the same
        if (self.left != self.right) and (len(self.decodedleft) == len(self.decodedright)):
            #Initiate a new Offset
            offset = Offset()

            #Save the differences in a list of offset
            for line in diffList:

                #if it found a difference
                if line[0] == '?':
                    offset.line= count - (len(diffresult.offsets) +1 )
                    offset.start = bytesread + line.index('^')
                    offset.size = line.count('^')

                    #add the offset in the list
                    diffresult.offsets.append(Offset(offset.line, offset.start, offset.size))

                #sum total of bytes read to calculate the offset position
                bytesread=bytesread+len(line)
                #sum the number of lines read
                count=count+1

            print

        #Unfortunately, all the attempts to serialize the list in JSON have failed..
        #I will try it more in a future version
        #For now, I will proceed with a not elegant way to convert it into JSON

        return diffresult.__str__()
Пример #2
0
    def test_DiffSameSizeDiffContents(self):
        diffresult = DiffResult()
        diffresult.decodedleft = validJSON
        diffresult.decodedright= validJSON[:-1] + "*" #Change the last char and keep the same size

        #create few offsets
        offsets = list()
        offset = Offset()

        offsets.append(Offset(10,100,2))
        offsets.append(Offset(20,400,3))
        offsets.append(Offset(25,460,1))

        diffresult.offsets = offsets

        self.assertEqual(diffresult.__str__(), diffresult.GetEqualSizeDiffResultMsg(),"Expected diff result message for diff json contents with same size")
Пример #3
0
 def test_DiffSizeMsg(self):
     diffresult = DiffResult()
     diffresult.decodedleft = validJSON
     diffresult.decodedright= validJSON + "aaa" #adding 3 bytes
     self.assertEqual(diffresult.__str__(), diffresult.GetDifferentSizeMsg(),"Expected different size message for different json sizes")
Пример #4
0
 def test_EqualResultMsg(self):
     diffresult = DiffResult()
     diffresult.decodedleft = validJSON
     diffresult.decodedright= validJSON
     self.assertEqual(diffresult.__str__(), diffresult.GetEqualResultMsg(),"Expected equal result message for same json contents")